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

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:
    5
    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:
    201
    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,847
    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:
    5
    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.