Nik Myers Nik Myers - 6 months ago 10
Android Question

RigidBody gravityScale not working

I have a

rigidBody
, it attached to game object like this:

obj = GameObject.Find ("spider");
body = obj.AddComponent<Rigidbody2D> ();
body.angularVelocity = 180f;


So it's moving by gravity influence down from the screen, but i want it to stop at some point, so i've made a condition :

if (obj.transform.position.y < 0) {
Debug.Log("Stop point");
body.gravityScale = 0.0f;
}


I see in logs message "Stop Point" which notifies me that gravity should be stopped, but nothing is happening and object still going down, what am i doing wrong?

Answer

GravityScale indicate how many the gravity affect to your object. If your object is in movement, its have velocity, and gravity to 0 only tell that not add force to your gameobject, not stop this. Think that with gravity 0 is similar to an object in outspace, but with velocity its never stop. Use Addforce or Set velocity to stop your object.

body.velocity = Vector3.zero;
body.angularVelocity = 0.0f;

//Ensure not move
body.Sleep()
Comments