How to enable rigidbody constraints only at start?
I want to freeze movement at x axis on start for several seconds, then allow movement. So i used Coroutine method to enable and disable rigidbody constraints. It is frozen on start, but not for seconds, during all the game x- axis movement is frozen. Here is the code:
void Start() {
StartCoroutine (Wait ());
}
void FixedUpdate () {
StopCoroutine (Wait ());
}
IEnumerator Wait() {
rb.constraints =RigidbodyConstraints.FreezePositionX;
yield return new WaitForSeconds (3);
}
Answer by metalted · May 31, 2019 at 10:37 AM
If you want to constrain the movement from the beginning, you could just check the checkbox on the rigidbody. Anyway, you are setting the constraints from the scripts, no problem there. You are not "unchecking it". You need some code to say: he, rb.constraints = "no constraints". Stopping the coroutine will not do that. You need to use RigidbodyConstraints.None for that.
So the easiest way to do this imo, is to check the checkbox for x position in the inspector for your rigidbody, then start your coroutine "Wait()" which will look like this:
IEnumerator Wait(){
yield return new WaitForSeconds(3);
rb.constraints = RigidbodyConstraints.None;
}
Your answer
Follow this Question
Related Questions
Attempt to unfreeze constraints using RigidbodyConstraints.None has no effect 0 Answers
I have a problem with constraining rotation of rigid bodies connected to hinge joints. 0 Answers
How do I unfreeze z rotation in a script and then freeze it again 1 Answer
Rigidbody 2D Freeze Position X bug? 0 Answers
Error CS0201 when trying to change RigidbodyConstraints 1 Answer