- Home /
How to have an object be plotted with an angle and altitude around an object
I have made a simple script to test the feasibility of this idea, but it doesnt work. #pragma strict var originX = 0; var originY = 0; var altitude = 100; var angle = 45; var relativeX = 0; var relativeY = 0; function Start () {
}
function Update () {
if (angle < 90 && angle > 1)
{
relativeX = Mathf.Sin(angle) * altitude;
relativeY = Mathf.Cos(angle) * altitude;
transform.position = Vector3(relativeX + originX,0,relativeY + originY);
}
else
{
angle = 0;
}
angle = angle + 0;
}
I realize this wont work with angles outside 1 through 90, this was just a test, hence the check
the script will plot the object around the other, but it appears that every angle increment it adds 60°, and will somehow happily go above 90°, and well above 360°. the object also randomly disappears which i assume has something to do with bad scripting.
I not clear what you are trying to do here. It looks like you want to plot a circular path around originX and originY. I don't see any code actually incrementing 'angle,' so I cannot figure out what is going wrong with that code. $$anonymous$$aybe a diagram and possibly more of your code would help.
i was wanting to plot an object with an angle and altitude around another object to create an "orbit" I realized I have been using degrees while mathf.sin() uses radians, but my ship is still disappearing randomly
Answer by robertbu · Mar 14, 2013 at 06:58 AM
First as for your code above, you have the Sin() and Cos() reversed for your polar to rectangular conversion. Here is your script with a few changes. Start with an empty scene and attach it to a cube:
var speed = 0.001;
var originX = 0.0;
var originY = 0.0;
var altitude = 4.0;
var angle : float = 0.0;
function Update () {
var relativeX = Mathf.Cos(angle) * altitude;
var relativeY = Mathf.Sin(angle) * altitude;;
angle = (angle + speed * Time.deltaTime) % (2.0 * Mathf.PI);
transform.position = Vector3(relativeX + originX,0,relativeY + originY);
}
Note that the are significantly easier ways to orbit object in Unity. One is to use Transform.RotateAround().
Another way:
Place an empty game object at the center of rotation.
Place your object to orbit in orbit at the right altitude.
Make the object in orbit a child of the empty game object by dragging and dropping it on the empty game object.
Rotate the empty game object with a simple script. Just call this each frame:
transform.Rotate(Vector3(0,10 * Time.deltaTime, 0);
Thank you so much! what does the "Time.deltaTime) % (2.0 * $$anonymous$$athf.PI)" do? does that make higher orbits slower than lower ones?
This code just increments the angle. By using using Time.deltaTime this way, I make the rotation independent of the frame rate, so that the rotation will happen at the same speed no matter what platform you run it on. The "% (2.0 $$anonymous$$athf.PI)" is probably not necessary. Angle is measured in radians which from 0 to 2 PI. This code just makes sure the angle is always in this range. It would almost certainly work without that code.