키보드 입력을 인식하기 위해서는 input 클래스의 GetKey() 메서드가 필요
Getkey() 메서드는 특정한 키의 코드를 입력받아, 해당 키를 누른 상태라면 true를, 해당 키가 눌러있지 않다면 false를 반환하는 메서드이다.
void Update()
{
if(Input.GetKey(KeyCode.W)){
this.transform.Translate(0, 1, 0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 0.05f;
void Update()
{
if(Input.GetKey(KeyCode.W)){
this.transform.Translate(0, speed, 0);
}
if(Input.GetKey(KeyCode.A)){
this.transform.Translate(-speed, 0, 0);
}
if(Input.GetKey(KeyCode.S)){
this.transform.Translate(0, -speed, 0);
}
if(Input.GetKey(KeyCode.D)){
this.transform.Translate(speed, 0, 0);
}
}
}
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
private void Start()
{
}
private void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 move = new Vector3(horizontal, vertical, 0f);
transform.Translate(move * moveSpeed * Time.deltaTime);
}
}
// 유니티가 wasd랑 방향키를 기본 지원
public float speed = 0.05f;