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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Horizontal movement slower than vertical movement, c# (SIMPLE)

Discussion in 'Scripting' started by tallieke, Jan 24, 2016.

  1. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37
    So heres my probably simple problem.. For my characters movement, i have a player controller script and a player motor script, it took me a while, but I've finally gotten them to work, except one thing. the horizontal movement is much slower than the vertical movement, and when i change my speed variable, it only affects vertical. What can i do to fix this?

    player controller script

    Code (CSharp):
    1.  
    2.     private PlayerMotor motor;
    3.  
    4.     void Start ()
    5.     {
    6.         motor = GetComponent("PlayerMotor") as PlayerMotor;
    7.     }
    8.  
    9.     void Update ()
    10.     {
    11.         //Calculate movement velocity as a 3d Vector
    12.         float _xMov = Input.GetAxisRaw("Horizontal");
    13.         float _zMov = Input.GetAxisRaw("Vertical");
    14.  
    15.         Vector3 _movHorizontal = transform.right * _xMov;
    16.         Vector3 _movVertical = transform.forward * _zMov;
    17.  
    18.         //final movement vector
    19.         Vector3 _velocity = (_movHorizontal + _movVertical.normalized * speed);
    20.  
    21.         //apply movement
    22.         motor.Move(_velocity);
    23.        
    24.     }
    25.  
    26.  
    27. }
    28.  
    and hers the player motor script

    Code (CSharp):
    1. [RequireComponent(typeof(Rigidbody))]
    2. public class PlayerMotor : MonoBehaviour {
    3.  
    4.     private Vector3 velocity = Vector3.zero;
    5.  
    6.     private Rigidbody rb;
    7.  
    8.     void Start ()
    9.     {
    10.         rb = GetComponent<Rigidbody> ();
    11.     }
    12.  
    13.     //Gets a movement vector
    14.     public void Move (Vector3 _velocity)
    15.     {
    16.         velocity = _velocity;
    17.     }
    18.  
    19.     //Run every physics iteration
    20.     void FixedUpdate ()
    21.     {
    22.         performMovement ();
    23.     }
    24.     //Perform movement based on velocity variable
    25.     void performMovement ()
    26.     {
    27.         if (velocity != Vector3.zero)
    28.         {
    29.             rb.MovePosition (transform.position + velocity * Time.fixedDeltaTime);
    30.         }
    31.     }
    32.    
    33.  
    34. }
    35.  
    any help is greatly appreciated... thakns :)
     
  2. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    I think the mistake is here.

    Code (CSharp):
    1.         //final movement vector
    2.         Vector3 _velocity = (_movHorizontal + _movVertical.normalized * speed);
     
  3. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37
    Do you know what i could do to fix it?
     
  4. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    Maybe, though I was hoping pointing it out to you might be enough help without giving away the answer.

    Think about what is happening in that formula:
    Code (csharp):
    1. _movHorizontal + _movVertical.normalized * speed
    Why are you treating Horizontal different from Vertical?
    Vertical seems to be getting normalized while Horizontal does not.
    Also, don't forget about BEDMAS rules when you multiply by speed.
     
    Roman_Keivan likes this.
  5. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37
    I feel pretty stupid now that I think about it, thanks for the quick answer, you helped me out lots :)