Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Why is my character moving forward even thought i am not pressing anything

Discussion in 'Scripting' started by TheStartOfGames, Apr 21, 2021.

  1. TheStartOfGames

    TheStartOfGames

    Joined:
    Apr 21, 2021
    Posts:
    3
    I have the code here

    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {

    public CharacterController controller;

    public float speed = 12f;





    void FixedUpdate()
    {
    float x = Input.GetAxis("Horizontal");

    float z = Input.GetAxis("Vertical");

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

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




    }
    }
     
  2. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    115
    well you are not multiplying transform.forward with the z value you read out, just fix this

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

    by writing

    Vector3 move = transform.right * x + transform.forward * z;
     
    PraetorBlue likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,010
    Whenever I see this question, and in the code I see GetAxis, often times it ends up being a gamepad/joystick connected to the computer which isn't properly calibrated.
     
  4. TheStartOfGames

    TheStartOfGames

    Joined:
    Apr 21, 2021
    Posts:
    3
    Alright, great i actually am just learning to code and my youtube "assistant" is brackeys but i think i missed something on my video heh. But thank you very ,uch for the help.
     
unityunity