유니티

[유니티 2D] 미로 - 유령이 플레이어 바라보고 쫓아오기

labj 2024. 3. 10. 02:15

Assets 폴더의 유령 이미지를 Scene 창에 끌어다 놓습니다. 

 

Assets 폴더의 유령 이미지를 Scene 창에 끌어다 놓습니다. 

 

 

Ghost 게임오브젝트가 car 게임오브젝트 쪽으로 움직입니다.

Rotate.cs C# 코드를 작성하고 Ghost 게임오브젝트에 연결 시킵니다.

* Rotate.cs

…
public class Rotate : MonoBehaviour
{
    // 전역변수
    public GameObject target;

    void Start()
    {        
    }
    
    void Update()
    {
        transform.position = Vector3.MoveTowards(transform.position, target1.transform.position, 0.01f);
        transform.rotation = Quaternion.Euler(0, 0, 0);
    }
}

 

 

Rotate.cs C# 코드를 Ghost 게임오브젝트에 연결 시키면, Ghost 게임오브젝트에 Rotate(Script) 컴포넌트가 생깁니다.

Rotate(Script)컴포넌트에 있는 Target 항목에 Car 게임오브젝트를 연결시킵니다.