- Home /
Changing walking speed in CharacterController.Move
I have a script:
private var moveDirection : Vector3 = Vector3.zero;
var speed = 3;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
Screen.showCursor = false;
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
controller.Move (moveDirection * Time.deltaTime);
transform.Rotate(Vector3(0, Input.GetAxis("Mouse X"), 0) * Time.deltaTime * 50);
if (Input.GetKeyDown (KeyCode.LeftShift))
{
speed += 10;
}
if (Input.GetKeyUp (KeyCode.LeftShift))
{
speed -= 10;
}
}
How can I change the movement speed when I press W, A, S and D (It works for sprint but not if I do it for the other keys)
I' trying to change the speed the character controller goes compared to the W, A, S and D buttons.
And do you mean the speed you get when you combine keys? Do you mean that sprint only works in a certain direction? $$anonymous$$aybe give an example of something that needs to happen?
I mean when you press S, the backward key, you go backwards much slower than when you press W, the forwards key.
I see where diagonal movement will speed up since you do not normalize 'moveDirection', but I don't see anything that would cause a backwards slow down. Are you hitting anything in your movement? Do you have any child object with colliders on the character?
Answer by robertbu · May 25, 2014 at 03:43 PM
Backwards will be when 'Input.GetAxis("Vertical")' is negative. So you can add between lines 11 and 12:
if (moveDirection.z < 0.0) {
moveDirection.z *= someFactor;
}
Where 'someFactor' will have a value between 0.0 and 1.0 for a slow down.
Your answer
Follow this Question
Related Questions
CharacterController sprint functionality. 1 Answer
How to let a GameObject generate force but not be affected by certain forces? 0 Answers
SimpleMove not working on Y axis? 1 Answer
What is the best way to move a character for an FPS? 1 Answer
Footstep sounds system with a Vr touchpad movement system 1 Answer