Search Unity

Problems with movement in Unity

Discussion in 'Scripting' started by gsturnz, Jun 17, 2019.

  1. gsturnz

    gsturnz

    Joined:
    Dec 13, 2017
    Posts:
    1
    I'm working on a top down view zombie wave type game kinda like dead ops arcade from call of duty. I have been trying to perfect the movement I had it where I wanted but I had the problem where if you went in a diagonal using for example: W + A you would go faster than desired. So read through some things watched a tutorial and fixed that problem but have a bigger one now. when my player rotates it changes the direction the keys moves the player unlike before where W, A, S, D was always forward, left, down, right. before the change i was using the AddForce() function and just getting inputs using Input.GetKey("W") for example i would also like to know that would stop me from porting game to mobile in the future thanks so much in advance for any help and here is my current code.
    also link to dead ops arcade for visual of what I am going for and why the movement will feel better this way.

    https://callofduty.fandom.com/wiki/Dead_Ops_Arcade

    Code (CSharp):
    1. using UnityEngine;
    2. [RequireComponent(typeof(playerMotor))]
    3. public class movement : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     private float speed = 5f;
    7.     private playerMotor motor;
    8.  
    9.     private void Start()
    10.     {
    11.         motor = GetComponent<playerMotor>();
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         //calculate movement velocity as a 3d vector
    17.         float xMov = Input.GetAxisRaw("Horizontal");
    18.         float yMov = Input.GetAxisRaw("Vertical");
    19.  
    20.         Vector3 movHorizontal = transform.right * xMov;
    21.         Vector3 movVertical = transform.forward * yMov;
    22.  
    23.         //final movement vecotor
    24.         Vector3 velocity = (movHorizontal + movVertical).normalized * speed;
    25.  
    26.         motor.Move(velocity);
    27.     }
    28. }


    and the motor class...


    Code (CSharp):
    1. using UnityEngine;
    2. [RequireComponent(typeof(playerMotor))]
    3. public class movement : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     private float speed = 5f;
    7.     private playerMotor motor;
    8.  
    9.     private void Start()
    10.     {
    11.         motor = GetComponent<playerMotor>();
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         //calculate movement velocity as a 3d vector
    17.         float xMov = Input.GetAxisRaw("Horizontal");
    18.         float yMov = Input.GetAxisRaw("Vertical");
    19.  
    20.         Vector3 movHorizontal = transform.right * xMov;
    21.         Vector3 movVertical = transform.forward * yMov;
    22.  
    23.         //final movement vecotor
    24.         Vector3 velocity = (movHorizontal + movVertical).normalized * speed;
    25.  
    26.         motor.Move(velocity);
    27.     }
    28. }