- Home /
Setting RigidBody.Constraints to None isn't reapplying Gravity
Rudimentary 'falling block' game. Two sprites currently, both with position and rotation 'frozen' in the RigidComponent2D. Gravity scale for both objects set to 1.
Goal: Pressing a key causes the block to fall.
Method: Keypress detected in Update() call, set block's RigidComponent2D constraints to 'None'. Result: Constraints are removed (confirmed by checking Inspector), but block doesn't fall.
Notes: If done without a keypress, block falls. If done by toggling 'freeze y' position in Inspector, block also falls.
What am I missing?
private Rigidbody2D _rigidBody;
// Start is called before the first frame update
void Start()
{
_rigidBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.Space)) {
//_rigidBody.constraints = RigidbodyConstraints2D.FreezePositionX;
_rigidBody.constraints = RigidbodyConstraints2D.None; //this gets called, but doesn't trigger the gravity to 'apply'
}
_rigidBody.constraints = RigidbodyConstraints2D.None; //this does work though
}
Try going to scene mode and manually moving the block a little. If it falls then, it was probably because the block was "asleep" and removing constraints didn't wake it up.
What would cause the object to be 'asleep' and how does one 'wake it up'?
Answer by fracticality · Nov 30, 2020 at 11:16 PM
Figured it out, thanks to @Owen-Reynolds!
The RigidBody2D component has a Sleeping mode property.
From the documentation (https://docs.unity3d.com/ScriptReference/Rigidbody2D-sleepMode.html):
"Sleeping is an optimisation that is used to temporarily remove an object from physics simulation when it is at rest. This property chooses whether the rigidbody should start off asleep, awake or have sleeping turned off altogether."
To resolve, I just had to change the Sleeping mode to 'Never Sleep':
Your answer
Follow this Question
Related Questions
Best way to keep an ai grounded? 1 Answer
Jumping Stopped Working. 2 Answers
Bike wobbles like pendulum after applying gravity 1 Answer
Js: UnityEngine.Rigidbody is required, how to fix? 2 Answers
How to set gravity for model? 1 Answer