실행 이미지

 

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class QuaternionTest : MonoBehaviour
{
    public Transform player; 
    public float Distance = 5.0f; // Player로부터의 거리
    public float Speed = 50.0f; // 도는 속도

    void Update()
    {
        OrbitPlayer();
    }

    void OrbitPlayer()
    {
        if (player == null) return;

        // Player 주위를 도는 오브젝트의 위치 계산
        float x = Mathf.Cos(Time.time * Speed) * Distance;
        float z = Mathf.Sin(Time.time * Speed) * Distance;
        transform.position = new Vector3(x, 0, z) + player.position;


        //Player를 바라봄
        transform.LookAt(player);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Enemy"))
        {
            Debug.Log("Hit!");
        }
    }

    private void OnDrawGizmos()
    {
        if (player == null) return;

        Gizmos.color = Color.white; 
        float theta = 0;
        float x = Distance * Mathf.Cos(theta);
        float z = Distance * Mathf.Sin(theta);
        Vector3 prevPos = player.position + new Vector3(x, 0, z);
        for (theta = 0.1f; theta < Mathf.PI * 2; theta += 0.1f)
        {
            x = Distance * Mathf.Cos(theta);
            z = Distance * Mathf.Sin(theta);
            Vector3 newPos = player.position + new Vector3(x, 0, z);
            Gizmos.DrawLine(prevPos, newPos);
            prevPos = newPos;
        }
        Gizmos.DrawLine(prevPos, player.position + new Vector3(Distance * Mathf.Cos(0), 0, Distance * Mathf.Sin(0)));

    }
}

QuaternionTest.cs

+ Recent posts