본문 바로가기

Unity/Unity Learn

Junior Programmer > Unit 1 - Player Control

https://learn.unity.com/project/unit-1-driving-simulation?uv=2020.3&pathwayId=5f7e17e1edbc2a5ec21a20af&missionId=5f71fe63edbc2a00200e9de0

 

Unit 1 - Player Control - Unity Learn

In this Unit, you will program a car moving side-to-side on a floating road, trying to avoid (or hit) obstacles in the way. In addition to becoming familiar with the Unity editor and workflow, you will learn how to create new C# scripts and do some simple

learn.unity.com

 

 

Time.deltaTime

The time in seconds it took to complete the last frame.

 

다른 프레임들 간에 시간의 변화를 가져오기 위한 것

전 프레임이 완료되기까지 걸린 시간을 말한다. 

컴퓨터의 성능이 느릴수록 값이 커져서 20프레임이든 60프레임이든 같은 속도를 보장할 수 있다.

 

void Update()
{
    //We'll move the vehicle forward
    //Time.deltaTime을 이용하여 차량의 이동속도를 보정
    transform.Translate(Vector3.forward * Time.deltaTime * 20);
}

 

 

 

 

 

 

Smooth the Camera with LateUpdate

Update() 메서드 카메라 위치 갱신에 사용할 경우 차량과 카메라가 동시에 움직이게 된다.

그래서 때때로 차량이 카메라보다 먼저 움직이고 카메라가 나중에 움직이는 경우가 발생하거나,

반대로 카메라가 먼저 움직이는 경우가 발생하여 화면이 떨리는 현상이 발생한다.

 

이러한 현상을 간단하게 방지하는 것은 카메라 위치 갱신을 호출하는 메서드를 LateUpdate()로 변경하면 된다.

Update() 메서드가 호출된 후에 LateUpdate()가 호출되어 호출되는 순서가 일정하여 떨려보이는 현상을 방지할 수 있다.

 

void LateUpdate()
{
    // 플레이어 뒤에 카메라를 위치시키기 위한 오프셋
    transform.position = player.transform.position + offset;
}

 

 

 

 

 

 

https://github.com/Myoungmin/UnityLearn-Create_with_Code-Prototype_1

 

GitHub - Myoungmin/UnityLearn-Create_with_Code-Prototype_1

Contribute to Myoungmin/UnityLearn-Create_with_Code-Prototype_1 development by creating an account on GitHub.

github.com