How to get 2 game objects rotate around another game object in unity c#,
Using cos and sin method. the 2 game objects are 2 planets that rotate around another game objects.
The 2 planets need to be space out apart so that you can get 4 planets or game objects rotate the middle object.
using UnityEngine;
using System.Collections;
public class Sun : MonoBehaviour {
public GameObject[] planets;
private int array_size = 2;
private Vector3 obj_pos;
public GameObject a_gameobject;
private float x, y, z;
private float t;
private GameSystem gs_script;
private int ring_count = 2;
void Awake() { Debug.Log ("SUN Script: AWAKE"); gs_script = GameObject.Find("GameSystem").GetComponent<GameSystem>(); }
void Start()
{
Debug.Log ("SUN Script: START");
array_size = 3;
planets = new GameObject[array_size];
for(int i = 0; i < planets.Length; i++)
{
obj_pos = gameObject.transform.position;
//a_gameobject = GameObject.Find("Planet");
GameObject temp_go = Instantiate (a_gameobject, obj_pos, Quaternion.identity) as GameObject;
temp_go.transform.position = obj_pos;
temp_go.gameObject.name = "Planet " + gs_script.planet_count;
planets *= temp_go;*
-
//Debug.Log("Planet " + i);*
-
//gs_script = GameObject.Find("GameSystem").GetComponent<GameSystem>();*
-
gs_script.planet_count = gs_script.planet_count + 1;*
-
}*
- }*
- // Update is called once per frame*
- void Update () {*
-
t = t + Time.deltaTime;*
-
//int i = 0;*
-
for(int i =0; i < planets.Length; i++)*
-
{*
_ PlanetRotate(planets , i);_
* }*
* }*
* void PlanetRotate(GameObject planet, int i)*
* {*
* Vector3 obj_pos;
_ x = gameObject.transform.position.x;_
_ y = gameObject.transform.position.y;_
_ z = gameObject.transform.position.z;_
_ x = Mathf.Cos (t * 2) + (x);_
_ y = 1;_
_ z = Mathf.Sin (t * 2) + (z);_
//obj_pos = (i, x, y, z);
_ planet.transform.position = new Vector3 (x, y, z);_
//planet.transform.position = obj_pos;
_/ x = gameObject.transform.position.x;
* y = gameObject.transform.position.y;*
* z = gameObject.transform.position.z;*
x = Mathf.Cos (t * 2) + (x);
* y = 1;*
z = Mathf.Sin (t * 2) + (z);
* planet.transform.position = new Vector3 (x, y, z);*
/_
_ }_
_ Vector3 setVerctorPos(int temp, float x, float y, float z)_
_ {_
_ int i = temp;_
_ if (i == 0) // ring 1*_
* {*
_ x = Mathf.Cos (t * 2) + (x);
* y = 1;*
z = Mathf.Sin (t * 2) + (z);
* }*
* if (i == 1) // ring 2*
* {*
x = 2*(Mathf.Cos (t * 2) + (x));
* y = 1;*
z = 2*(Mathf.Sin (t * 2) + (z));
* }*
* if(i == 2) // ring 3*
* {*
x = 3*(Mathf.Cos (t * 2) + (x));
* y = 1;*
z = 3*(Mathf.Sin (t * 2) + (z));
* }*
* Vector3 a_cord = new Vector3 (x, y, z);
return a_cord;
_ }*
}