How to crouch a FPS Controller?
Hi there can somebody help me? I want to crouch the FPS Controller (default FPS Controller from standard assets of Unity 5.3) but I can't find a way out... I tried this:
if (Input.GetKeyDown (KeyCode.C)) {
CameraWithTorch.transform.position = Vector3.Lerp (CameraWithTorch.transform.position,crouchPosition.position,Time.deltaTime * smooth);
check = true;
}
else{
if (check == true) {
CameraWithTorch.transform.position = Vector3.Lerp (CameraWithTorch.transform.position,cameraposition.position,Time.deltaTime * smooth);
check=false;
}
}
but this is incomplete/not working because it works only on the Camera of the Controller... Can someone give me a tip?
ps sorry for the English
Answer by b1gry4n · Apr 11, 2016 at 06:41 PM
GetKeyDown is only true for the first frame the key was pressed down. Lerp will require continuous updates to smoothly transition between your positions. I suggest you take the logic of your crouch out of the input section of your code.
if (Input.GetKeyDown(KeyCode.C))
{
if (crouching )
{
crouching = false;
}
else {
crouching = true;
}
}
if(crouching) {
CameraWithTorch.transform.position = Vector3.Lerp(CameraWithTorch.transform.position, crouchPosition.position, Time.deltaTime * smooth);
}
else {
CameraWithTorch.transform.position = Vector3.Lerp(CameraWithTorch.transform.position, cameraposition.position, Time.deltaTime * smooth);
}
You will have to resize your character controller to your "crouch height" using height
http://docs.unity3d.com/ScriptReference/CharacterController-height.html
When you resize its height, it will probably seem like it jumps into the air (its because its resizing it from its center), you can offset this by adjusting the transforms position at the same time. For example...
if (crouching)
{
character.height = 1.0f;
transform.position = new Vector3(transform.position.x, transform.position.y - 0.5f, transform.position.z);
}
else {
character.height = 2.0f;
transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
}
Answer by SynisterJoker · Apr 15, 2016 at 02:58 PM
Thank you very much, with character.height it was easier to solve the problem. This is my script now :
if (check)
{
character.height = 1.61f; //crouch height
transform.position = new Vector3(transform.position.x, transform.position.y - 0.4f, transform.position.z);
}
else {
character.height = 3.22f; //player height
//transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
}
I "commented" this line :
//transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
because of the resizing the character looks like it jumps (like what you said)..
Your answer
Follow this Question
Related Questions
MoveTowards is curving for no reason 0 Answers
Character Controller not going up terrain hill 1 Answer
Character Rotation 0 Answers
Setup an animation script, but now mouselook is not working? 0 Answers
how to move only one object ? 1 Answer