- Home /
Changing the Speed of CharacterController.Move(Vector3.forward)
Hi Guys , I am new to Unity and I am trying to build a small FPS project.I am sorry if the doubt sounds silly.
I am using the CharacterController.Move(Vector3.forward) to move the player forward when i press w. (Assume currently player is pointing to the north direction for example).
But when my player points to the opposite direction(south direction) , pressing the w key causes my player to move backwards instead of forward. How can this be fixed ?
Also I wanted to slow down the player movement a little according to what i need. How can this be achieved ?
Answer by MattG54321 · Jul 24, 2017 at 08:11 PM
Try using Transform.TransformDirection on your Vector3.forward. This will make it relative to your character's orientation. To change your speed, just multiply the move vector by how fast you want to go. I also recommend multiplying it by Time.deltaTime to make it frame rate independent. Ultimately, you'd have something like this:
private CharacterController controller;
void Start () {
//You don't really want to use GetComponent in Update because it's slow
//And you only need to do it once.
controller = GetComponent<CharacterController>();
}
void Update () {
float moveSpeed = 5f;
Vector3 relativeForward = transform.TransformDirection(Vector3.forward);
controller.Move(relativeForward * moveSpeed * Time.deltaTime);
}
I highly recommend you check out the documentation on CharacterController.Move. They've got a great example there. Also, if you don't want to worry about Time.deltaTime or gravity, check out CharacterController.SimpleMove.
Your answer
![](https://koobas.hobune.stream/wayback/20220612130927im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move Script Issues 0 Answers
How to go about constraining a player to a path? 0 Answers
Isometric diagonal movement (not 45 degrees) in a 2D game C# 0 Answers
Character controller speed problem 1 Answer