- Home /
Changing Rigidbody.Constraints on Keypress
I'm currently trying to make it so that when I run into a ball that ball's position goes along with my player's position and then the rigidbody on the ball will freeze constraints. I have that working through this code:
void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "ball") { //whatever is controlling the ball's transform is now equal to the ballPickUp collider's transform other.gameObject.transform.parent = gameObject.transform; other.gameObject.rigidbody.constraints = RigidbodyConstraints.FreezeAll; hasBall = true; } else hasBall = false;
but I'm trying to get it so that the transformations will unfreeze when I press space and allow me to apply a force to the ball's rigidbody. I got that working through this script on the ball:
if (ball)
{
if(Input.GetKeyDown("space"))
{
transform.parent = null;
rigidbody.constraints = RigidbodyConstraints.None;
rigidbody.AddForce(passDirection * speed * Time.deltaTime);
Debug.Log("Ball recognizes it needs to be somewhere...");
}
}
but then if I collect the ball again through the collision that sets the ball transform the RigidbodyConstraints.FreezeAll doesn't seem to work. I've tried putting RigidbodyConstraints.FreezeAll under Update like so:
if(hasBall)
{
ball.gameObject.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
}
but if I do this then it will freeze the constraints between my GetKeyDown and this GetKeyUp statement that is on the same script as my OnTriggerEnter():
if (Input.GetKeyUp("space"))
{
print("throwBall");
heldTime = 0.0f;
hasBall = false;
}
Sorry if it's all confusing and I know my code is super messy. If you could help me that would be great! If you need any clarifications I'd be glad to do that. Thanks!
Your answer
Follow this Question
Related Questions
Remove rigidbody component 1 Answer
Rigidbody constraints not working with parent/child relationship 2 Answers
when I fully constrain my walls in the rigidbody the player goes straight through the wall 1 Answer
Constrain character to series of intersecting paths? 1 Answer
Is it possible to constrain an object's rotation in worldspace? 2 Answers