- Home /
Axial tilt and moons
I found a script that makes an object spin and orbit the origin.
#pragma strict
function Start () {
}
var PlanetRotateSpeed : float = -25.0;
var OrbitSpeed : float = 10.0;
function Update() {
// planet to spin on it's own axis
transform.Rotate(transform.up * PlanetRotateSpeed * Time.deltaTime);
// planet to travel along a path that rotates around the sun
transform.RotateAround (Vector3.zero, Vector3.up, OrbitSpeed * Time.deltaTime);
}
What I'd like to do is have moons orbit moving planets and tilt the planets' Y axes. When I try tilting a planet that uses this script, it rotates chaotically. Does anyone have a script that allows axial tilt and orbiting moving objects?
Is it rotating about itself chaotically or around the orbit chaotically. If its the former, I think I know what's up.
It's rotating chaotically; it orbits as expected.
Answer by hoy_smallfry · May 07, 2013 at 10:46 PM
The documentation on RotateAround() states that it affects both the position and rotation of the object that it is used on in world coordinates. Rotate(), on the other hand, rotates the object around its center on local coordinates by default. Therefore, this script is rotating the object in two different directions and by different coordinate systems.
The real problem, though, is that Transform.up is in world coordinates, yet its being used to transform in local coordinates. You don't notice anything when the moon is upright in the world, because Vector.up and transform.up are both (0, 1, 0) in this state. Therefore, the orbit's rotation axis doesn't transform the direction of transform.up to anything other than (0, 1, 0). The transform.up is then used as a local coordinate, which also conveniently happens to be the local vector for up (0, 1, 0). So the result appears that the moon's rotation axis is fine.
However, the moment you tilt it, the bad logic becomes apparent because the orbit 's rotation axis modifies the direction of transform.up to something that is not (0, 1, 0). Since it no longer happens to be the local vector for up, its now rotating the moon on a new axis every frame; hence, your chaotic movement.
There are two ways to solve this. First one is to bring the moon rotation to world coordinates as well, so that it uses transform.up in the proper coordinates:
// planet to spin on it's own axis
transform.Rotate(transform.up * PlanetRotateSpeed * Time.deltaTime, Space.World);
or, you can keep the moon rotation in local corrdinates, and just change the axis to the local vector for up:
// Vector.up in local *is always* transform.up in world
transform.Rotate(Vector.up * PlanetRotateSpeed * Time.deltaTime);
Both of them give the same output, but one fix does it in reference to local coordinates, the other in world coordinates.
Hope that helps!
That fixed my rotation problem, but how can I set the moon to orbit a planet that is orbiting the sun?
If you look at the documentation for RotateAround(), the first parameter you give it is the point it rotates around and the second is the axis of rotation. This function is world coordinates only, so you want to make sure that the values you pass to it are in world coordinates as well.
You just need to specify that you want to use the position of another game object ins$$anonymous$$d of Vector3.zero
, and then specify any special axis as the second argument.
Are you familiar with using references to other game objects in a script?
You need to make a public variables in your script, like this:
var centerObject : GameObject = null;
var orbitAxis : Vector3 = Vector3.up;
Center object is a GameObject type so any game objects be set to it, and the orbit axis will define how you want the object to rotate. When you create a public variables like this, they will appear in the inspector view for every game object that has this script. Since the centerObject
variable is set to null in the script, the value in the inspector will be "None".
You can then set whatever game object you want to that variable by dragging it from the hierarchy view to the variable in the inspector view, and it will reference the object (aka will link to it).
From there, you just change this line of code:
transform.RotateAround (centerObject.transform.position, orbitAxis, ...
And now, you have something that will orbit around any object in around any axis that you set them to from the editor.
Got that figured out in a different thread. It works perfectly now.
Your answer
![](https://koobas.hobune.stream/wayback/20220613101803im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a moon orbit your map 0 Answers
Orbit Collision Axis 0 Answers
Planetoid Gravity 1 Answer
Rotating around to a point 1 Answer
help with my orbit camera script 0 Answers