【한짱 비망록】 버튼을 누르고 있는 동안 오브젝트를 특정 위치로 이동시킨다
1. 처음에
이 기사는 Unity 초보자의 내가 기억한 것을 잊지 않는 메모와 같습니다.
2. 하는 일

위의 gif와 같이, 버튼을 누르고 있는 동안은 특정의 위치까지 이동시켜, 떼어 놓으면 초기 위치로 돌아가도록 한다. 이 동영상에서는 2개의 버튼으로 상하 이동, 놓으면 중앙으로 돌아가게 되어 있습니다.
그것을 응용하여 만든 게임이 여기입니다.

이를 응용한 게임
3. 스크립트
Player.csusing UnityEngine;
public class Player : MonoBehaviour
{
public float speed = 1; //移動スピード
private float step;
private Vector3 direction1 = new Vector3(0, 0, 0); //ボタンを押してない時の位置(初期位置)
private Vector3 direction2 = new Vector3(0, 2f, 0); //上ボタンを押した時に移動する位置
private Vector3 direction3 = new Vector3(0, -2f, 0); //下ボタンを押した時に移動する位置
void Start()
{
this.gameObject.transform.position = direction1; //ゲーム開始時に初期位置に移動
}
void Update()
{
//上ボタンを押した時
if (Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow))
{
step = speed * Time.deltaTime;
this.gameObject.transform.position = Vector3.MoveTowards(transform.position, direction2, step);
}
//下ボタンを押した時
else if (Input.GetKey(KeyCode.DownArrow) && !Input.GetKey(KeyCode.UpArrow))
{
step = speed * Time.deltaTime;
this.gameObject.transform.position = Vector3.MoveTowards(transform.position, direction3, step);
}
//ボタンを押してない時
else
{
step = speed * Time.deltaTime;
this.gameObject.transform.position = Vector3.MoveTowards(transform.position, direction1, step);
}
}
}
위 스크립트에서 키보드의 오른쪽 화살표 키를 누르면 direction2
위치. 왼쪽 화살표 키를 누르면 direction3
위치. 아무것도 누르지 않은 상태라면 초기 위치( direction1
)로 이동합니다.
이동 속도는 스크립트에서 speed
의 값을 변경하거나 첨부된 객체의 검사기에서 변경하십시오.
이 스크립트를 조작하고 싶은 객체에 첨부하면 아래의 gif 이미지와 같은 동작이 됩니다. ( speed = 10
의 경우 )

Player.cs //上ボタンを押した時
if (Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow))
{
...
}
//下ボタンを押した時
else if (Input.GetKey(KeyCode.DownArrow) && !Input.GetKey(KeyCode.UpArrow))
{
...
}
이 if문의 조건에 !Input.GetKey(KeyCode.DownArrow)
및 !Input.GetKey(KeyCode.UpArrow
를 넣고 있는 것은, 버튼이 동시 눌러졌을 경우의 대책입니다.
4. 정리
이상 Unity 초보자 메모
Reference
이 문제에 관하여(【한짱 비망록】 버튼을 누르고 있는 동안 오브젝트를 특정 위치로 이동시킨다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Hanchan-K/items/e9dd204457fd894c1c2f
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)

위의 gif와 같이, 버튼을 누르고 있는 동안은 특정의 위치까지 이동시켜, 떼어 놓으면 초기 위치로 돌아가도록 한다. 이 동영상에서는 2개의 버튼으로 상하 이동, 놓으면 중앙으로 돌아가게 되어 있습니다.
그것을 응용하여 만든 게임이 여기입니다.

이를 응용한 게임
3. 스크립트
Player.csusing UnityEngine;
public class Player : MonoBehaviour
{
public float speed = 1; //移動スピード
private float step;
private Vector3 direction1 = new Vector3(0, 0, 0); //ボタンを押してない時の位置(初期位置)
private Vector3 direction2 = new Vector3(0, 2f, 0); //上ボタンを押した時に移動する位置
private Vector3 direction3 = new Vector3(0, -2f, 0); //下ボタンを押した時に移動する位置
void Start()
{
this.gameObject.transform.position = direction1; //ゲーム開始時に初期位置に移動
}
void Update()
{
//上ボタンを押した時
if (Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow))
{
step = speed * Time.deltaTime;
this.gameObject.transform.position = Vector3.MoveTowards(transform.position, direction2, step);
}
//下ボタンを押した時
else if (Input.GetKey(KeyCode.DownArrow) && !Input.GetKey(KeyCode.UpArrow))
{
step = speed * Time.deltaTime;
this.gameObject.transform.position = Vector3.MoveTowards(transform.position, direction3, step);
}
//ボタンを押してない時
else
{
step = speed * Time.deltaTime;
this.gameObject.transform.position = Vector3.MoveTowards(transform.position, direction1, step);
}
}
}
위 스크립트에서 키보드의 오른쪽 화살표 키를 누르면 direction2
위치. 왼쪽 화살표 키를 누르면 direction3
위치. 아무것도 누르지 않은 상태라면 초기 위치( direction1
)로 이동합니다.
이동 속도는 스크립트에서 speed
의 값을 변경하거나 첨부된 객체의 검사기에서 변경하십시오.
이 스크립트를 조작하고 싶은 객체에 첨부하면 아래의 gif 이미지와 같은 동작이 됩니다. ( speed = 10
의 경우 )

Player.cs //上ボタンを押した時
if (Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow))
{
...
}
//下ボタンを押した時
else if (Input.GetKey(KeyCode.DownArrow) && !Input.GetKey(KeyCode.UpArrow))
{
...
}
이 if문의 조건에 !Input.GetKey(KeyCode.DownArrow)
및 !Input.GetKey(KeyCode.UpArrow
를 넣고 있는 것은, 버튼이 동시 눌러졌을 경우의 대책입니다.
4. 정리
이상 Unity 초보자 메모
Reference
이 문제에 관하여(【한짱 비망록】 버튼을 누르고 있는 동안 오브젝트를 특정 위치로 이동시킨다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Hanchan-K/items/e9dd204457fd894c1c2f
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
using UnityEngine;
public class Player : MonoBehaviour
{
public float speed = 1; //移動スピード
private float step;
private Vector3 direction1 = new Vector3(0, 0, 0); //ボタンを押してない時の位置(初期位置)
private Vector3 direction2 = new Vector3(0, 2f, 0); //上ボタンを押した時に移動する位置
private Vector3 direction3 = new Vector3(0, -2f, 0); //下ボタンを押した時に移動する位置
void Start()
{
this.gameObject.transform.position = direction1; //ゲーム開始時に初期位置に移動
}
void Update()
{
//上ボタンを押した時
if (Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow))
{
step = speed * Time.deltaTime;
this.gameObject.transform.position = Vector3.MoveTowards(transform.position, direction2, step);
}
//下ボタンを押した時
else if (Input.GetKey(KeyCode.DownArrow) && !Input.GetKey(KeyCode.UpArrow))
{
step = speed * Time.deltaTime;
this.gameObject.transform.position = Vector3.MoveTowards(transform.position, direction3, step);
}
//ボタンを押してない時
else
{
step = speed * Time.deltaTime;
this.gameObject.transform.position = Vector3.MoveTowards(transform.position, direction1, step);
}
}
}
//上ボタンを押した時
if (Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow))
{
...
}
//下ボタンを押した時
else if (Input.GetKey(KeyCode.DownArrow) && !Input.GetKey(KeyCode.UpArrow))
{
...
}
이상 Unity 초보자 메모
Reference
이 문제에 관하여(【한짱 비망록】 버튼을 누르고 있는 동안 오브젝트를 특정 위치로 이동시킨다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Hanchan-K/items/e9dd204457fd894c1c2f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)