<유니티> 게임 오브젝트 이동

상태
완료
담당자
날짜
숫자
0
using UnityEngine; public class Movement2D : MonoBehaviour { private void Update() { // 새로운 위치 = 현재 위치 + (방향 * 속도) //transform.position = transform.position + new Vector3(1, 0, 0) * 1; transform.position += Vector3.right * 1 * Time.deltaTime; //Time.deltaTime이란? //이전 Update() 종료부터 다음 Update() 시작까지의 시간 //즉, 업데이트 사이의 시간 //ex) 1분(60초)에 Update()가 60번 호출된다면 TimeUpdateTime은 1 //이동거리 = 방향 * 속도 * Time.deltaTime //input class : 입력에 관한 모든 프로퍼티, 메서드 제공 } }
C#
복사
using UnityEngine; public class Movement2D : MonoBehaviour { private float MoveSpeed = 5.0f; // 이동속도 private Vector3 MoveDirection = Vector3.zero; // 이동 방향 private void Update() { //Negative left, a : -1 //Positive right, d : 1 //Non : 0 float x = Input.GetAxisRaw("Horizontal");//좌우 이동 //Negative down, s : -1 //Positive up, w : 1 //Non : 0 float y = Input.GetAxisRaw("Vertical"); //위아래 이동 //이동방향 설정 MoveDirection = new Vector3 (x, y,0); //새로운 위치 = 현재 위치 + (방향 * 속도) transform.position += MoveDirection * MoveSpeed * Time.deltaTime; //float value = Input.GetAxisRaw(”단축키명”); //긍정(Positive) : +1, 부정 (Negative) : -1 , 대기 : 0 //float value = Input.GetAxis(”단축키명”); //GetAxisRaw()는 키를 누르면 바로 1 or -1이 되지만 GetAxis()는 키를 누르고 있으면 0에서 1 or -1로 서서히 증가한다 (부정도 마찬가지) } }
C#
복사