- Home /
Crouching and falling through floor
Hello,
I am using the CharacterMotor.js and FPSInputController.js (basics) as movement scripts.
I created a basic game and just want the character to crouch now.
From reading various posts this is the script I created.
function Update ()
{
if (Input.GetKeyDown ("c"))
{
transform.position.y -= 0.5;
transform.localScale.y = 0.25;
}
if (Input.GetKeyUp ("c"))
{
transform.position.y += .7;
transform.localScale.y = 2.0;
}
}
My character starts a scale y = 2 (though 3 would be better)
I need to know how to make it so after hitting the "c" button to return to normal size (2/3) I do not fall through the floor. What do I need to fix?
It is completely flat.
I messed around with the controls a bit more and if I scale the character to Y=1 it is fine because after I release "c" (from it crouching) there really is no true crouch.
In other words while yes the character is crouching down to .25 for it to go back to 1.0 it is fine.
When it exceeds the 1.0 is when it falls through the floor because it is trying to 'grow' up and down at the same time.
If that all makes sense.
ok so you've completely fixed the problem.
DON'T use "-=" and "+=" ...
actually just SET the values. so, set them to 1, 0.5 or whatever you want.
I did not fix the problem because I need the character to actually resume the height of 2 or 3. I was just testing it out.
If I remove the "+ or -" the character will immediately fall through the floor.
O$$anonymous$$ let me explain. Firstly as I say do not use the "+=" or "-=" format. Just set them using "="
then of course, you must set the values CORRECTLY.
only YOU can figure out what they should be as you have the scene in front of you.
IF IT IS FALLING THROUGH THE FLOOR, $$anonymous$$A$$anonymous$$E THE "POSITION" HIGHER.
so if you try position.y = 3, and it falls through the floor, next you should try "4"
And so on
O$$anonymous$$ ?
Good luck