부드러운 레인 변경
Coddy의 Unity 게임: 스카이 레인 코스 레슨. 20개 중 2번째.
러너가 한 프레임 만에 레인에서 레인으로 점프합니다. 작동은 하지만 순간이동하는 것처럼 보입니다. 눈으로 따라갈 수 있도록 레인을 바꾸는 데 잠시 시간이 걸려야 합니다.
Mathf.MoveTowards(current, target, maxStep)는 숫자를 목표를 향해 최대 maxStep만큼 이동시키며, 목표를 지나치지 않습니다. 매 프레임 속도에 Time.deltaTime을 곱한 값을 이동량으로 지정해 호출하면 프레임 속도와 관계없이 초당 해당 속도만큼 이동합니다.
float x = Mathf.MoveTowards(transform.position.x, targetX, laneSpeed * Time.deltaTime);laneSpeed가 16이면 2.5단위의 레인 변경에 약 0.16초가 걸립니다. 빠르지만 움직임이 눈에 보입니다.
챌린지
쉬움laneWidth아래에public float laneSpeed = 16f;를 추가하세요.float x = targetX;를 위에 있는Mathf.MoveTowards줄로 바꾸세요.- ‘플레이’를 누르고 레인을 바꿔 보세요.
직접 해보기
using UnityEngine;
public class Runner : MonoBehaviour
{
public float laneWidth = 2.5f;
public int lane;
private CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) lane = Mathf.Max(-1, lane - 1);
if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) lane = Mathf.Min(1, lane + 1);
float targetX = lane * laneWidth;
float x = targetX;
Vector3 move = new Vector3(x - transform.position.x, 0f, 0f);
controller.Move(move);
}
}
Unity 게임: 스카이 레인의 모든 레슨
직접 연습해 보세요: Unity 플레이그라운드