I have a problem with my script, originally it was using parenting to create a relationship between the child and parent objects and it was perfect. I would run rotatearound each child object giving it the parent’s position. However, this parenting relationship makes the child inherit the scale and rotation rate of the parent when i literally only want it to inherit the parent.
Now I’ve changed my code to allow the child objects to inherit the Transform of the parent now and each update they’re runing a “self” rotate and a rotatearound in update, but every time i try to calculate the difference in position between the targetTransform (the parent) and the child, i end up with either all the objects fixed in a single position, or all the children clearly orbiting the wrong coordinates and flying off the camera.
using UnityEngine;
using System.Collections;
public class CO : MonoBehaviour {
string COName;
int COOffset; //Distance from Parent object, relative of course
int COdegrees; //How many degrees per second will the object rotate
Transform targetTransform; //This is the location of the "parent" object to use for local coordinate settings
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (targetTransform != null) { //If the parent is null, we're dealing with the Star, so we don't need to rotate it around it's parent.
transform.RotateAround (targetTransform.position, Vector3.up, 50 * Time.deltaTime);
}
transform.Rotate (Vector3.up, COdegrees * Time.deltaTime); //Rotate clockwise around the parent
}
public void positionObject(int tCOOffset, int tCOdegrees, Transform tTransform) //Set the initial position on creation, will be updated later.
{
COdegrees = tCOdegrees;
COOffset = tCOOffset;
targetTransform = tTransform;
transform.position = new Vector3(0,0,COOffset); //Set initial position of child object
}
public void nameObject(string tName)
{
COName = tName;
}
public Transform getSelf()
{
return this.transform;
}
void OnMouseDown ()
{
print (COName);
print (transform.position);
print (transform.parent);
}
}
I think you could update your question to include your example with the solar system. That will make it easier to help you
– Mikael-H