- Home /
Crouch script problem!
I've made a very simple FPS game with all kinds of cool stuff, but the only problem is that my crouch script is having some kind of issues. If I crouch my character falls through floor! Is there a way to pervent it from doing that? Here is the script:
function Update ()
{
if (Input.GetKeyDown ("c"))
{ transform.localScale.y = 0.25;}
if (Input.GetKeyUp ("c"))
{ transform.localScale.y = 1.0;}
}
Answer by Xedfire 1 · Mar 26, 2011 at 02:39 PM
Your script works fine, it's just when you come out of the crouching state, the player expands downwards. This means that it would fall through the floor. I modified your script a little, and when attached to Unity's First Person Controller Prefab, it works fine.
Here is the script:
function Update () {
if (Input.GetKeyDown ("c")) { transform.position.y -= 0.5;
transform.localScale.y = 0.25;
}
if (Input.GetKeyUp ("c")) { transform.position.y += 0.7;
transform.localScale.y = 1.0;
} }
Basically, all that I have added is two lines that adjust the players y position accordingly. If it does not work with your gameObject, try tinkering with some of the variables.
I have been looking for a simple script to do just this till I was going crazy. This one WOR$$anonymous$$S. Thanks a lot!!
Your answer
Follow this Question
Related Questions
Smooth Crouch Movement Issue 0 Answers
Crouching and falling through floor 0 Answers
crouch script 4 Answers
Player falls through terrain when using crouching script? 0 Answers
FP Camera how to crouch and headbob at the same time 0 Answers