- Home /
How to set custom RigidbodyConstraints?
Hello,
I still have my issue with the particles and the sphere. See link for question. However I've decided to try it from a different perspective and not use the fixedjoint which doesn't give me the results I want.
So now I'm gonna try and keep the particle in position. For this I (think I) need the rigidbody.constraints.
The particles constraints start with the following settings for constraints
freeze position x(false) y (false) z(true)
freeze rotation x (true) y (true) z(false) // this ensures 2d movement
after it hits the sphere it should remain in it's position, but it should still move a bit like it's wiggling or something. So I would like to set the constraints to
freeze position x(true) y(true) z(true)
freeze rotation x(true) y(true) z(false)
Debug.Log returns 56 with the initial setting. Changing it to one of the prefixed settings would return that enum.
How to set the constraints to a custom setting?
And how to change it back, because they will only stick to each other for about 3 seconds...
Answer by Huacanacha · Nov 06, 2013 at 07:55 AM
It's all there in the docs for Rigidbody.constraints. You use bitwise OR | to specify which dimensions to constrain, and any you don't specify will be unconstrained. You can store the current constraints in a RigidbodyConstraints type, then reinstate them when you're finished.
RigidbodyConstraints previousConstraints = rigidbody.constraints;
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY; // Constrain only on X and Y rotation axis
// ... later ...
rigidbody.constraints = previousConstraints; // set to previous state
In your case you can also pre calculate the set of constraints you want to apply (all positions and X and Y rotations locked):
private RigidbodyConstraints wiggling = RidigBodyConstraints.FreezePosition | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY;
Edit: changed from int type to RigidbodyConstraints type for storing the constraints.
thanks, this solved my problem
it's better to use a RigidbodyConstraints type to store it though
Indeed it is! I'll update my answer so as not to confuse people.
Your answer
![](https://koobas.hobune.stream/wayback/20220613121258im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Setting rigidbody's force to 0 1 Answer
Rigid body speed not increasing. 1 Answer
Rigidbody2D won't stop moving. 2 Answers
Remove rigidbody component 1 Answer