게임 세상 속 모든 정보는 실시간으로 변화한다.
따라서 게임은 주기적으로 갱신 처리를 실시하는데, 이 때 초당 프레임을 활용한다.
Update() 메서드는 이러한 프레임 하나마다 반복실행된다.
이러한 Update() 메서드를 활용해서 입력 감지 및 갱신이 가능합니다.
예시 입력 코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
private Rigidbody PlayerRigidbody;
public float speed = 8f;
void Start() {
PlayerRigidbody = GetComponent<Rigidbody>();
}
void Update() {
float xInput = Input.GetAxis("Horizontal");
float zInput = Input.GetAxis("Vertical");
float xSpeed = xInput * speed;
float zSpeed = zInput * speed;
Vector3 newVelocity = new Vector3(xSpeed, 0f, zSpeed);
playerRigidbody.Velocity = newVelocity;
}
public void Die() {
gameObject.SetActive(false);
}
}