forked from renaissanceCoder/Simple-Scripting-Series
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Orbit.cs
51 lines (30 loc) · 842 Bytes
/
Orbit.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Orbit : MonoBehaviour {
public float xSpread;
public float zSpread;
public float yOffset;
public Transform centerPoint;
public float rotSpeed;
public bool rotateClockwise;
float timer = 0;
// Update is called once per frame
void Update () {
timer += Time.deltaTime * rotSpeed;
Rotate();
}
void Rotate() {
if(rotateClockwise) {
float x = -Mathf.Cos(timer) * xSpread;
float z = Mathf.Sin(timer) * zSpread;
Vector3 pos = new Vector3(x, yOffset, z);
transform.position = pos + centerPoint.position;
} else {
float x = Mathf.Cos(timer) * xSpread;
float z = Mathf.Sin(timer) * zSpread;
Vector3 pos = new Vector3(x, yOffset, z);
transform.position = pos + centerPoint.position;
}
}
}