이동을 배웠고, 회전을 배웠으니 응용해서 위치이동을 구현해본다
FPS를 인식할 수 있다.deltaTime을 쓰면 프레임 차이로 인한 연산결과 차이를 없앨 수 있다C#의 Console.Read특정 장치(키보드, 마우스, 게임패드)의 입력을 감지
여러 플랫폼에 대응이 어려움. (모바일, VR 등)
GetKey : 누르는 동안 trueGetKeyDown : 누를 때 한 번만 trueGetKeyUp : 누르고 있다가 떼었을때 한 번 truebool isInput = Input.GetKey(KeyCode.Space);
KeyCode는 ConsoleKey와 같은 열거형이다GetMouseButton(0~2) trueGetMouseButtonUp(숫자) : 누를 때 한 번만 trueGetMouseButtonDown(숫자) : 누르고 있다가 떼었을 때 한 번만 trueprivate void InputByDevice()
{
// 키보드 입력감지
if (Input.GetKey(KeyCode.Space))
{
Debug.Log("Space key is perssing");
}
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Space key is down");
}
if (Input.GetKeyUp(KeyCode.Space))
{
Debug.Log("Space key is up");
}
// 마우스 입력감지
if (Input.GetMouseButton(0))
{
Debug.Log("Mouse left button is pressing");
}
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Mouse left button is down");
}
if (Input.GetMouseButtonUp(0))
{
Debug.Log("Mouse left button is up");
}
}



이와같이 미리 할당되어잇는것을 확인할 수 있다

Fire1 : 키보드(Left ctrl), 마우스(Left button), 조이스틱(Button0)으로 정의함GetButton
if (Input.GetButton("Fire1"))
{
Debug.Log("Fire1 is pressing");
}
if (Input.GetButtonDown("Fire1"))
{
Debug.Log("Fire1 is down");
}
if (Input.GetButtonUp("Fire1"))
{
Debug.Log("Fire1 is up");
}
GetAxis
Horizontal), Z축 (Vertical) 값을 받는다-1.0 ~ 1.0 사이의 float 타입을 반환GetAxisRaw
-1, 0, 1로 반환Horizontal(수평) float x = Input.GetAxis("Horizontal");
if (x != 0)
{
Debug.Log($"Horizontal Axis {x}");
}
Vertical(수직) float y = Input.GetAxis("Vertical");
if (y != 0)
{
Debug.Log($"Vertical Axis {y}");
}
InputSystem 패키지를 이용한 입력방식private void OnFire(InputValue value)
{
// Fire 버튼 입력에 반응하는 OnFire 메시지 함수
bool input = value.isPressed;
Debug.Log($"OnFire input {input}");
}
private void OnMove(InputValue value)
{
// Move 축 입력에 반응하는 OnMove 메시지 함수
Vector2 input = value.Get<Vector2>();
Debug.Log($"OnMove input {input}");
}
Input Manager를 사용했다. 상하 키에 함수 하나, 좌우 키에 함수 하나를 구현해서, 업데이트마다 두 함수를 수행한다.deltaTime으로 구현했다public class TankMover : MonoBehaviour
{
[SerializeField] float moveSpeed;
[SerializeField] float rotateSpeed;
// Update is called once per frame
void Update()
{
Move();
Rotate();
}
private void Move()
{
float input = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * moveSpeed * input * Time.deltaTime);
}
void Rotate()
{
float input = Input.GetAxis("Horizontal");
// 자동차 처럼 회전하기
//if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
//{
// input = -input;
//}
transform.Rotate(Vector3.up * rotateSpeed * input * Time.deltaTime);
}
}
상하좌우 키를 누를 때, 해당 방향으로 진행을 할 수 있도록 한다. 차체 또한 진행방향으로 회전시킨다. 동일하게 이동 속도, 회전 속도를 조절할 수 있게 한다A, D 키를 누르면 포탑이 각각 좌, 우로 회전한다. 회전 속도를 조절가능할 수있게 한다deltaTime으로 구현했다public class AdvancedTankMover : MonoBehaviour
{
[SerializeField] float tankMoveSpeed;
[Range((float)0.01, (float)1)]
[SerializeField] float tankRotInterpolation;
private Vector3 tankInput;
private float magnitude;
[SerializeField] Transform turret;
[SerializeField] float turretRotSpeed;
void Update()
{
TankMove1();
TurretRotation();
}
// GetKey로 탱크 움직임 구현
void TankMove1()
{
tankInput = Vector3.zero;
if(Input.GetKey(KeyCode.UpArrow))
{
tankInput += Vector3.forward;
}
if (Input.GetKey(KeyCode.DownArrow))
{
tankInput += Vector3.back;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
tankInput += Vector3.left;
}
if (Input.GetKey(KeyCode.RightArrow))
{
tankInput += Vector3.right;
}
if(tankInput == Vector3.zero)
{
return;
}
if(tankInput.magnitude > 1f)
{
magnitude = 1f;
}
else
{
magnitude = tankInput.magnitude;
}
tankInput = tankInput.normalized;
transform.Translate(tankInput * magnitude * tankMoveSpeed * Time.deltaTime, Space.World);
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(tankInput), tankRotInterpolation);
}
void TurretRotation()
{
if (Input.GetKey(KeyCode.A))
{
turret.Rotate(Vector3.down * turretRotSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
turret.Rotate(Vector3.up * turretRotSpeed * Time.deltaTime);
}
}
}
Input Manager를 사용하면 포탑 회전 키와 겹치기 때문에 분리를 위해 Input class를 사용했다.
