[Unity] 2D 객체 이동

Southbig·2023년 3월 7일
0

객체 이동

Time.deltaTime

두 컴퓨터에서 캐릭터 이동을 했을 때,
캐릭터의 Update() 1회 당 이동거리를 5m라고 할 때,

  • 사양이 좋지 않은 컴퓨터는 60초에 Update()가 60회 호출
  • 사양이 좋은 컴퓨터는 60초에 Update()가 120회 호출

Time.deltaTime 이란 ?

이전 Update() 종료부터 다음 Update() 시작까지의 시간
즉, 업데이트 사이의 시간
(1분에 Update()가 60번 호출된다면 Time.deltaTime은 1)

사양이 좋이 않은 컴퓨터는 60초에 Update()가 60번 호출
(Time.deltaTime의 값은 1)

사양이 좋은 컴퓨터는 60초에 Update()가 120번 호출
(Time.deltaTime의 값은 0.5)

이동거리 = 방향(Direction) 속도(Speed) Time.deltaTime

Input

Edit -> Project Settings -> Input Manager

Input Manager에 등록 되어있는 Horizontal, Vertical을 사용한다

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()
    {
        float x = Input.GetAxisRaw("Horizontal");
        float y = Input.GetAxisRaw("Vertical");

        moveDirection = new Vector3(x, y, 0);
        transform.position += moveDirection * moveSpeed * Time.deltaTime;
    }
}

float value = Input.GetAxisRaw("단축키명")
긍정(Positive) : +1
부정(Negative) : -1
대기 : 0

float value = Input.GetAxis("단축키명")

GetAxisRaw()는 키를 누르면 바로 1 or -1이 되지만
GetAxis()는 키를 누르고 있으면 0에서 1 or -1로 서서히 증가한다

profile
즐겁게 살자

0개의 댓글