- Home /
Crouching makes my player unable to move.
So I am trying to make my character crouch. However it stops the ability to move all together. Can anyone figure out why.
if (isStanding == true) {
float forwardSpeed = Input.GetAxis ("Vertical") * movementSpeed;
float sideSpeed = Input.GetAxis ("Horizontal") * movementSpeed;
// Determine movement speed in every direction
Vector3 speed = new Vector3 (sideSpeed, verticalVelocity, forwardSpeed);
speed = transform.rotation * speed;
characterController.Move (speed * Time.deltaTime);
}
//Crouching
if (Input.GetKeyDown(KeyCode.C) && characterController.isGrounded)
{
// Determine movement speed in every direction
float forwardSpeed = Input.GetAxis ("Vertical") * crouchingSpeed;
float sideSpeed = Input.GetAxis ("Horizontal") * crouchingSpeed;
Vector3 speed = new Vector3 (sideSpeed, verticalVelocity, forwardSpeed);
speed = transform.rotation * speed;
characterController.Move (speed * Time.deltaTime);
Crouch (forwardSpeed,sideSpeed);
}
public void Crouch(float forwardSpeed, float sideSpeed) {
if (!isCrouching) //Crouch
{
isCrouching = true;
isProne = false;
isStanding = false;
characterController.height = crouchHeight;
// characterController.center = Vector3(0, -0.5, 0);
// tempCamera.transform.localPosition.y -= crouchHeight;
}
else //Stand back up
{
isCrouching = false;
characterController.height = charHeight;
isStanding = true;
isProne = false;
// characterController.center = Vector3(0, 0, 0);
// tempCamera.transform.localPosition.y += crouchHeight;
}
}
Answer by Ymrasu · Oct 25, 2015 at 04:51 PM
You are only trying to move your character the moment C is pressed, you need an if statement similar to your isStanding check:
if (Input.GetKeyDown(KeyCode.C) && characterController.isGrounded)
{
Crouch (forwardSpeed,sideSpeed);
}
if (isCrouching == true)
{
// Determine movement speed in every direction
float forwardSpeed = Input.GetAxis ("Vertical") * crouchingSpeed;
float sideSpeed = Input.GetAxis ("Horizontal") * crouchingSpeed;
Vector3 speed = new Vector3 (sideSpeed, verticalVelocity, forwardSpeed);
speed = transform.rotation * speed;
characterController.Move (speed * Time.deltaTime);
}
although I recommend you just change your movement speed when you crouch and then change it back when you stand back up and reuse the same movement code, instead of one for each movement type.
Well this brings me back to my other issue, it will only change its speed on the time I clicked on the button like you said. Any ideas?
Your answer
Follow this Question
Related Questions
Character Controller - My movement speed changes dont stick. 1 Answer
What is the best way to move a character for an FPS? 1 Answer
Continuously rotate a gameobject on 1 axis perpendicularly to another gameobject's normal? 1 Answer
How do I make a jumping and ledge-grabbing system, similar to TLoZ OOT in unity 3d? 0 Answers