게임개발/게임플랫폼 응용 프로그래밍
플레이어의 주변을 회전
pudding81
2024. 3. 5. 00:22
using UnityEngine;
public class RotateAroundPlayer : MonoBehaviour
{
// 플레이어 오브젝트를 참조할 변수
public Transform player;
// 회전 속도를 조절할 변수
public float speed = 10f;
// 반지름을 저장할 변수
public float radius = 3f;
// 각도를 저장할 변수
private float angle = 0f;
// 매 프레임마다 실행되는 함수
private void Update()
{
// 각도를 시간과 속도에 비례하게 증가시킴
angle += speed * Time.deltaTime;
// 각도를 라디안으로 변환
float radian = angle * Mathf.Deg2Rad;
// 원의 좌표를 구함
float x = Mathf.Cos(radian) * radius;
float z = Mathf.Sin(radian) * radius;
// 물체의 위치를 플레이어의 위치에 원의 좌표를 더한 값으로 설정
transform.position = player.position + new Vector3(x, 0, z);
// 물체가 플레이어를 바라보게 함
transform.LookAt(player);
}
}