- Home /
Character Controller - My movement speed changes dont stick.
I am trying to make my crouch script change the movement speed. The way I do it now, it only works for the 0.0001 second or so that the keycode is clicked. However I do need crouching to stay: -Click C while standing = Crouch -Click C again while crouching = Stand back up
Any ideas?
Crouching if (Input.GetKey(KeyCode.C) && characterController.isGrounded) { Crouch(forwardSpeed,sideSpeed); }
public void Crouch(float forwardSpeed, float sideSpeed) {
if (!isCrouching) //Crouch
{
isCrouching = true;
isProne = false;
isStanding = false;
characterController.height = crouchHeight;
forwardSpeed = Input.GetAxis ("Vertical") * crouchingSpeed;
sideSpeed = Input.GetAxis ("Horizontal") * crouchingSpeed;
Vector3 speed = new Vector3 (sideSpeed, verticalVelocity, forwardSpeed);
speed = transform.rotation * speed;
characterController.Move (speed * Time.deltaTime);
// 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;
forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
}
}
**Normal Speed**
forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
Vector3 speed = new Vector3 (sideSpeed, verticalVelocity, forwardSpeed);
speed = transform.rotation * speed;
characterController.Move (speed * Time.deltaTime);
Answer by Socapex · Oct 26, 2015 at 07:35 PM
You are calling the method only once, when the user presses the key. Instead you can set a bool to true when ctrl+c, and then check that in update. Pseudo code:
// Start standing.
bool isCrouching = false;
if (Input.GetKey(KeyCode.C) && characterController.isGrounded) {
isCrouching = !isCrouching;
}
void Update() {
if (isCrouching) {
Crouch(forwardSpeed,sideSpeed);
}
}
You are not calling any function for when its false, hence I cant stand back up. I had it similar like this before, and yes the speed changes and so work, but I cannot figure out how to make it stand up again. Every attempt I did, screwed it all up again.
That is pseudo-code, you shouldn't copy-paste it as-is. Code a different method for when its false.
Oh my bad, ill look into it then. Had no idea what pseudo-code ment.
Your answer
Follow this Question
Related Questions
Crouching makes my player unable to move. 2 Answers
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
Custom C# Crouch Script 1 Answer