Input 클래스에 GetAxis("Horizontal")을 입력하여 좌우 입력값을 받아온다. (입력값 왼쪽 : -1, 오른쪽 : 1)
프레임당 이동 거리를 구한다 (입력값 * 스피드 * 방향 * 입력한 시간)
GetComponent는 Object가 여러개일 경우 제일 먼저있는 Object를 가져온다.
Transform Translate에 값을 입력하여 Object를 이동시킨다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class PlayerControl : MonoBehaviour { public float speed = 15.0f; private Transform myTransform = null; // Start is called before the first frame update void Start() { myTransform = GetComponent<Transform>(); } // Update is called once per frame void Update() { float axis = Input.GetAxis("Horizontal"); Vector3 moveAmout = axis * speed * -Vector3.right * Time.deltaTime; myTransform.Translate(moveAmout); } } | cs |
'유니티' 카테고리의 다른 글
[유니티] Object 생성 (0) | 2019.03.05 |
---|---|
[유니티]Object 삭제 (0) | 2019.03.04 |
[유니티] Object에 Script연결 (0) | 2019.03.01 |
[유니티] 2D 배경 설정 (0) | 2019.02.28 |
[유니티] 메인카메라 3D -> 2D 변경 (0) | 2019.02.27 |