39 lines
1.1 KiB
C#
39 lines
1.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class orbitAndViewParent : MonoBehaviour
|
|
{
|
|
public float orbitSpeed = 20f;
|
|
// public float setAngle = 90f;
|
|
|
|
private Transform originPoint;
|
|
private float angle;
|
|
private float radius;
|
|
private float height;
|
|
|
|
void Start()
|
|
{
|
|
originPoint = transform.parent;
|
|
|
|
// Calculate radius and height from initial position
|
|
Vector3 localPos = transform.localPosition;
|
|
radius = Mathf.Sqrt(localPos.x * localPos.x + localPos.z * localPos.z);
|
|
height = localPos.y;
|
|
|
|
// Calculate starting angle
|
|
angle = Mathf.Atan2(localPos.z, localPos.x) * Mathf.Rad2Deg;
|
|
// angle += setAngle;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
angle += orbitSpeed * Time.deltaTime;
|
|
|
|
float x = Mathf.Cos(angle * Mathf.Deg2Rad) * radius;
|
|
float z = Mathf.Sin(angle * Mathf.Deg2Rad) * radius;
|
|
|
|
transform.localPosition = new Vector3(x, height, z);
|
|
transform.LookAt(originPoint.position);
|
|
}
|
|
} |