Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Character not moving where the camera is facing

Discussion in 'Scripting' started by masterbuilder335, Jan 20, 2021.

  1. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    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.
     
  2. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    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);
    }
    }
     
  3. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    I am using a Mac btw
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    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://forum.unity.com/threads/using-code-tags-properly.143875/

    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.
     
    masterbuilder335 likes this.
  5. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    Sorry for not replying, It looks to me that the character is not being rotated for some reason?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    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.
     
    masterbuilder335 likes this.
  7. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    ??? It is a nonzero direction in .Move() right? Also I don't have a RigidBody.
     
  8. Omniglitch

    Omniglitch

    Joined:
    May 29, 2017
    Posts:
    37
    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.

    Code (CSharp):
    1. void Update()
    2. {
    3.     float x = Input.GetAxis("Horizontal");
    4.     float z = Input.GetAxis("Vertical");
    5.  
    6.     Vector3 move = transform.right * x + transform.forward * z;
    7.  
    8.     move = Quaternion.Euler(0f, facing, 0f) * move;
    9.  
    10.     controller.Move(move * speed * Time.deltaTime);
    11. }
    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.
     
    masterbuilder335 likes this.
  9. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    aha I will try this whenever I get the chance (is brackey's video out of date then?)
     
  10. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    hmm so what do I set facing? public float Facing = ?
     
  11. Omniglitch

    Omniglitch

    Joined:
    May 29, 2017
    Posts:
    37
    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:
    Code (CSharp):
    1. facing += Input.GetAxis("Mouse X");
    Better yet, you'll want a mouse sensitivity property, so that line should be:
    Code (CSharp):
    1. facing += Input.GetAxis("Mouse X") * mouseSensitivity;
    Declare the mouseSensitivity member variable at the top of the class with this line:
    Code (CSharp):
    1. [SerializeField] private float mouseSensitivity = 1f;
    You can then tweak that value in the inspector until the mouse responds the way you want.
     
    masterbuilder335 likes this.
  12. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    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.