- Home /
RigidbodyConstraints.FreezePositionY on button press
Hey guys im trying to make a script that lets me essentially float at when i hold a button down, and when i release it i want to stop floating. my character floats and moves perfectly when i freeze position y in the rigidbody constraints, so i was trying to set up a code that froze y when i press a button and unfreeze when i let go. this is what i whipped up but i didn't work. #pragma strict
var originalConstraints : RigidbodyConstraints;
function Awake(){
originalConstraints = rigidbody.constraints;
}
{
if (Input.GetKeyDown("E"));
{
rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
}
else
{
rigidbody.constraints = originalConstraints;
}
}
i'm fairly new to unity but im learning fast. i think theres just a few things i dont quite grasp. i also would like to somehow store the ability to float in like a true/false thing. so i can make it an item pickup. thanks so much for your help future helpers.
Well the problem with the script might be that you are using "Get$$anonymous$$eyDown" rather than "Get$$anonymous$$ey." Down only returns true on the frame that the key goes down. get$$anonymous$$ey is true when the key is down and false when the key is up.
However you may find that freezing the position isn't the best fit for what you want. Alternatively you could turn gravity off for that ridgidbody. Or if you are applying some other directional force, simply Adding an equal and opposite force would cancel it out.
Your answer
![](https://koobas.hobune.stream/wayback/20220613151311im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody Floating Problem? 2 Answers
Can a object without a rigidbody be triggered by a trigger? 1 Answer
custom rigidbody movement problem 2 Answers
Problem with Rigidbody control script! (collision with perpendicular walls problem) 1 Answer
How can i make my RigidBodyFPSController to move when Is Kinematic is true ? 1 Answer