Unity 플레이어 이동

용준·2023년 8월 15일
0

Unity

목록 보기
3/19

이동 함수는 여러가지 기법이 존재합니다.
일반적인 상황에서는 GetAxix와 GetAxisRaw를 자주 사용합니다.
GetAxisRaw는 GetAxis와 미세한 차이가 있으며 다음과 같습니다.

Input.GetAxis(string axisName)
부드러운 이동을 할 때 필요로 합니다.
-1.0f부터 1.0f까지 범위의 값을 반환합니다.

Input.GetAxisRaw(string axisName)
즉시 이동을 할 때 필요로 합니다.
-1, 0, 1 세 가지 값 중 하나를 반환합니다.

파라미터(string axisName)는 Input Manager의 입력축(Axes)의 네임을 할당받습니다.


  • 이동 스크립트 예시
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
    public float moveSpeed = 10.0f;
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
        transform.position += new Vector3(h, 0, v) * Time.deltaTime * moveSpeed;
    }
}

0개의 댓글