- project setting -> Input Manager에서 오브젝트 조작키 설정가능
float value = Input.GetAxisRaw("단축키명"); // 긍정 +1, 부정 -1, 대기 0
float value = Input.GetAxis("단축키명"); // GetAxisRaw()는 키를 누르면 바로 1 or -1이 되지만, GetAxis()는 서서히 증가
// Movement.cs
using System.Collections;
using System.Collections.Generic;
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;
}
}
'Unity' 카테고리의 다른 글
Unity 2D exercise (shooting game) (0) | 2021.07.13 |
---|---|
Unity 2D basic #5 (Instantiate) (0) | 2021.07.04 |
Unity 2D basic #4 (Trigger) (0) | 2021.07.03 |
Unity 2D basic #3 (Collision) (0) | 2021.07.03 |
Unity 2D basic #1 (Initialize & Update) (0) | 2021.07.03 |