How to get 2 game objects rotate around another game object in unity c#
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[i] = 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], 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;
}
}
I am not entirely sure whether this will help but it might make it a whole lot easier using the Transform.RotateAround()
function as described in docs
Answer by soey_0 · Jul 05, 2017 at 05:48 AM
I kind of figured it out, a sung with 4 planets rotating the sun in 4 quadrents.
if (x0 > 1 || x0 < 1) { x0 += 0.5f; } x0 = Mathf.Cos (t 2); y0 = 1; z0 = Mathf.Sin (t 2); planets[0].transform.position = new Vector3 (x0, y0, z0);
//float x1, y1, z1;
if (x1 > 1 || x1 < -1) { x1 += 0.2f; } x1 = -Mathf.Cos (t * 2); y1 = 1; z1 = -Mathf.Sin (t * 2);
planets[1].transform.position = new Vector3 (x1, y1, z1);
if (x2 > -0.5f || x2 < -0.5f) { x2 += 0.1f; } x2 = Mathf.Cos(t * 3); y2 = 1; z2 = Mathf.Sin (t * 3);
planets[2].transform.position = new Vector3 (x2, y2, z2);
if (x3 > -0.5f || x3 < 0.5f) { x3 += 0.3f; } x3 = -Mathf.Cos(t * 4); y3 = 1; z3 = -Mathf.Sin (t * 4);
planets[3].transform.position = new Vector3 (x3, y3, z3);
Thanks any way.
Your answer
