Rotation Help (432041)

Hello All.

I am relatively new to Unity and have been trying to get some basic rotation going for a simple two body system (think space orbits) where a planet it rotating a sun. I have implemented a sun planet with both having a rigidbody and have successfully implemented some “gravity” between the planet and the sun. However, when I try to get my planet to rotate (orbit) around the sun I simply can’t get my planet to move any meaningful distance. I have tried using Quaternions, transform.Rotation and neither seems to work. The main parts of my script are as follows:

function Update()
{
  
  yAngle += rotyAngle;
  if ( yAngle > 360 )
  {
  	yAngle -= 360;
  }
  
  if (yAngle < -360 )
  {
  	yAngle += 360;
  }
  
// Slowly rotate the object around its X axis at 1 degree/second.
//	transform.Rotate(Vector3.right * Time.deltaTime);

// ... at the same time as spinning relative to the global
// Y axis at the same speed.
//	this.transform.Rotate(Vector3.up * Time.deltaTime * rotyAngle, Space.World);

	this.transform.Rotate(0,yAngle,0, Space.World);	
  
    Debug.Log ("My position is " + this.transform.position + "  Target is located at " + orbitTarget.transform.position + " My rotation is " + this.transform.rotation + " yAngle is " + yAngle + " rotyAngle is " + rotyAngle);
  
}

rotyAngle is a public variable that I use to modify the current rotation angle by each update cycle. What I see in the debug log is that my position is not changing and my y-rotation values cycle between -0.1, 0.0, 0.1 consistently. I have my main camera looking straight down the y-axis so I am viewing my set up from the top. So I am thinking I should be seeing the planet move as well as seeing the values change in the log.

I have read through the documentation, searched the forum and Answers areas (which is where some of the commented code above came from) and not found anything that addresses this. I don’t think it should be this difficult but I just don’t see what I am missing. Any help would be appreciated.

What object is that script attached to? And are the star and the planet ‘related’ in the hierarchy (that is, is one the child of the other)?

Also, keep in mind that Transform.rotation is a quaternion, not a set of angles.

Thanks for the reply.

The script is attached to the planet object. The planet is not a child of the star nor vice versa. Should the planet be a child of the star?

I am using Transform.Rotate because it has a form which accepts angle inputs as parameters.

Would transform.rotation be better? I tried working with Quaternions but had no better luck. I may have tried that but, frankly, I have tried so many things that I don’t remember them all.

Transform.Rotate() rotates the object about an axis passing through its origin (local or world, as specified by the optional second argument to the function). As such, it would be suitable for causing a planet to rotate, but not to orbit another object (not in the way you’re trying to do it, at least).

Also, mixing rigid bodies and direct manipulation of an object’s transform will often give incorrect results. (I think it would work ok to manipulate an object’s orientation if its rigid body has its rotation ‘frozen’, but otherwise you’ll probably want to use a rigid body or manipulate the orientation directly, but not both.)

As for causing one object to orbit another using the built-in physics system, that might be a little iffy (since the physical behavior of the objects involved is only an approximation, orbits may collapse or expand over time). Another option would be dispense with the physics system and move and orient the objects by modifying their transforms directly. In this case though, you’ll probably need to use another method for causing one object to ‘orbit’ another. (One way you could do it would be to have an intermediary, non-visual ‘arm’ object that’s a child of the star and a parent of the planet. Another option would be to make the planet a child object of the star, and then move it procedurally along an elliptical path in local space.)

Thanks for the information. I have to travel today but I will modify what I am doing as soon as possible and let you know the results.

Thank again Jesse. I was able to get things running the way I was wanting by updating the position manually based on your input. At this point, rather than using a hidden object, I have tagged my star and am doing a search to for it in my script. Once I have found it then I calculate a radius from its position and use that radius to update my orbiting position. Don’t know if that will continue to work as I expand my system but I am going to play around with it and see.