I am trying to get a gameobject (planet) to orbit another gameobject (blackhole) in gradually smaller orbits until the planet collides with the blackhole.
This is my current code:
var BlackHoleCenter : Transform;
function FixedUpdate(){
transform.RotateAround (BlackHoleCenter.transform.position, Vector3(1, 0, 0), 200 * Time.deltaTime);
transform.LookAt(BlackHoleCenter);
rigidbody.AddForce(transform.forward*50);
}
If I comment out the transform.RotateAround line, the planet approaches the blackhole gradually as desired, but if I allow the transform.RotateAround line to run, the planet orbits, but never approaches the blackhole.
Why is this?
2 Answers
2
You mixed direct movement(RotateAround) with physic driven movement(AddForce). I recommend to use just direct movement, because you specify all values and the movement isn't really physic based.
var BlackHoleCenter : Transform;
function FixedUpdate(){
transform.RotateAround (BlackHoleCenter.transform.position, Vector3(1, 0, 0), 200 * Time.deltaTime);
transform.LookAt(BlackHoleCenter);
transform.Translate(Vector3.forward*50*Time.deltaTime);
}
You may need to adjust your "50" because it's no longer a force, it's a speed now.
You could also use physics for it by using:
var BlackHoleCenter : Transform;
function FixedUpdate()
{
transform.LookAt(BlackHoleCenter);
rigidbody.AddForce(transform.forward*50);
rigidbody.AddForce(transform.right*50);
}
This will make the rigidbody be purely physics based meaning you can alter its trajectory during gameplay using other obects/forces.
Wow, thank you.
– anon46463775