Character not moving where the camera is facing

Hello, I am new to coding and was watching Brackey’s fps Character Controller tutorial and it looked as if it was the same but when I tried it and hit W it moved in a set direction, same for A S D also.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;

public float speed = 12f;

// Update is called once per frame
void Update()
{
float x = Input.GetAxis(“Horizontal”);
float z = Input.GetAxis(“Vertical”);

Vector3 move = transform.right * x + transform.forward * z;

controller.Move(move * speed * Time.deltaTime);
}
}

I am using a Mac btw

Then I speculate it is not the same. The code above should be local movement (based on its use of transform.right and transform.forward) regardless of which way the above script’s transform is facing.

If it doesn’t work, then one of the following may be true:

  1. the character is not being rotated (but maybe the camera is?)
  2. the transform above is not being rotated
  3. the transform above is not the character’s transform
  4. something else?

When you post code, please use code tags: https://discussions.unity.com/t/481379

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

1 Like

Sorry for not replying, It looks to me that the character is not being rotated for some reason?

1 Like

As soon as you give CharacterController a nonzero direction in .Move(), it will turn to that heading. Make sure you don’t have something else preventing or overwriting that. Don’t use a Rigidbody if you are using a CharacterController, for instance. Those two are not compatible.

2 Likes

??? It is a nonzero direction in .Move() right? Also I don’t have a RigidBody.

1 Like

The move vector is being calculated based on the global right and forward vectors, so movement is going to be relative to global forward. All you need to do is rotate that vector to face in direction your character is facing.

void Update()
{
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    move = Quaternion.Euler(0f, facing, 0f) * move;

    controller.Move(move * speed * Time.deltaTime);
}

You’ll need to add a float called facing, and in your update function add code for changing the facing based on input from the mouse or keyboard.

2 Likes

aha I will try this whenever I get the chance (is brackey’s video out of date then?)

1 Like

hmm so what do I set facing? public float Facing = ?

1 Like

The facing is the rotation around the Y axis in degrees, so a number from 0 to 360, but you can go above or below that range and it will wrap around (i.e. -10 is the same as 350). In your Update() function you will update the facing variable with a line like:

facing += Input.GetAxis("Mouse X");

Better yet, you’ll want a mouse sensitivity property, so that line should be:

facing += Input.GetAxis("Mouse X") * mouseSensitivity;

Declare the mouseSensitivity member variable at the top of the class with this line:

[SerializeField] private float mouseSensitivity = 1f;

You can then tweak that value in the inspector until the mouse responds the way you want.

2 Likes

ok sorry, but the facing doesn’t wrap around after 360 when I am in game and the camera is moving it will go up to 400 or higher and it is W A S D is still set, but that is because I haven’t coded in to rotate the vector to where I am facing.

1 Like