- Home /
cannot have all constraints on in rigidbody?
I have a script that is meant to have the players, rigidbodies, rotation always locked, and when you click, but only while clicking, the position is frozen, but when I freeze the position it unfreezes rotation, is there a way to solve this?
void Start()
{
rb.constraints = RigidbodyConstraints.FreezeRotation;
time = starttime;
dash = false;
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
fist1.velocity = (Vector3.forward * speed * punch);
rb.constraints = RigidbodyConstraints.FreezePosition;
}
}
if you want to freeze the rigidbody position, you could set the velocity to vector3.zero in the FixedUpdate function. That should keep it still while allowing rotation?
Answer by Hellium · Aug 10, 2020 at 09:51 PM
Rigidbody contraints are flags
void Start()
{
rb.constraints = RigidbodyConstraints.FreezeRotation;
time = starttime;
dash = false;
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
fist1.velocity = (Vector3.forward * speed * punch);
rb.constraints |= RigidbodyConstraints.FreezePosition; // Freeze position & rotation
}
else if (Input.GetButtonUp("Fire1"))
{
rb.constraints &= ~RigidbodyConstraints.FreezePosition; // Unfreeze position
}
}
Another solution:
void Start()
{
rb.constraints = RigidbodyConstraints.FreezeRotation;
time = starttime;
dash = false;
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
fist1.velocity = (Vector3.forward * speed * punch);
rb.constraints = RigidbodyConstraints.FreezeAll;
}
else if (Input.GetButtonUp("Fire1"))
{
rb.constraints = RigidbodyConstraints.FreezeRotation;
}
}
Your answer
Follow this Question
Related Questions
How do I query the RigidbodyConstraints? 3 Answers
Multiple Cars not working 1 Answer
Attempt to disable constraints using RigidbodyConstraints.None has no effect 0 Answers
Rigidbody: Freeze All vs InKinematic 0 Answers
Can you unfreeze a rigidbody.constraint position as you can freeze them? 2 Answers