- Home /
How do I make a planet get sucked into a blackhole ?
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?
Answer by Bunny83 · Mar 18, 2011 at 03:03 AM
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.
Answer by Grim_Darknight · Jul 24, 2013 at 10:22 AM
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.
Your answer
Follow this Question
Related Questions
How could I use AddForce() towards another Object without using transform.forward 1 Answer
Add force look at 1 Answer
Rigid body rotation question 1 Answer
Facing the target 0 Answers