Search Unity

Why the object moves faster in 45 degree than in 90 degree?

Discussion in 'Getting Started' started by SebastianLeeCN, Jun 7, 2018.

  1. SebastianLeeCN

    SebastianLeeCN

    Joined:
    Apr 26, 2018
    Posts:
    2
    I've already multiplied the vector with 0.7071 and it still doesn't work.


    Code (CSharp):
    1. public float speed;
    2.     float a = 0.7071f;
    3.  
    4.     void Update()
    5.     {
    6.         if (Input.GetKey(KeyCode.W))
    7.         { transform.Translate(new Vector3(0,speed * Time.deltaTime ));
    8.         }
    9.         if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
    10.         {
    11.             transform.Translate(new Vector3(speed * Time.deltaTime * a, speed * Time.deltaTime * a));
    12.         }
    13.     }
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,620
    Assume 'speed' is 2 and let's leave out 'deltaTime' and 'a' for simplicity:

    Let's assume you move sidewards, 90 degrees:
    Code (CSharp):
    1. step = Vector3(0, 2, 0)
    2. magnitude = sqrt(step.x * step.x + step.y * step.y + step.z * step.z)
    3. magnitude = sqrt(0*0 + 2*2 + 0*0)
    4. magnitude = 2
    You would move 2 units.


    Now lets assume you move diagonally, 45 degree would give a movement vector of:
    Code (CSharp):
    1. step = Vector3(2, 2, 0)
    2. magnitude = sqrt(step.x * step.x + step.y * step.y + step.z * step.z)
    3. magnitude = sqrt(2*2 + 2*2 + 0*0)
    4. magnitude = 2.82
    You would move 2.82 units, which appears faster.
     
  3. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Peter77's answer is all you really need.

    However, one way to overcome this issue is as follows (note, this isn't the most elegant, but hoping that it's easy to understand):
    1. Each Update(), work out where the player wants to move by creating a temporary 'direction' Vector3, and adding to each each frame. For example:
      Code (CSharp):
      1. private Vector3 direction;
      2.  
      3. Update()
      4. {
      5.    direction = Vector3.Zero;
      6.    if(Input.GetButton("Right"))
      7.       direction += new Vector3(1,0,0);
      8.  
      9.    if(Input.GetButton("Up"))
      10.       direction += new Vector3(0,1,0);
      11.  
      12. ...
      13.  
    2. At the end of the Update(), use Vector3.Normalize to get the final direction with a length of 1.
      Code (CSharp):
      1. Update()
      2. {
      3. ...
      4.    direction = Vector3.Normalize(direction);}
    3. Now you have a direction, multiply it by your desired speed and the usual Time.deltaTime;

    Now, like I say, hopefully this is an easily understandable approach to follow after reading Peter77's answer. But less us know if this doesn't make sense.
     
  4. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    I do not know if is your case but in general, if you tell an object to move 1 unit right (sideways) + one unite up, then you finish moving 45° Right Up more than one unit diagonal.

    Looking at your code, If you press "D" and "W" at the same time, then will execute both lines of your code.

    Code (CSharp):
    1.  
    2.     transform.Translate(new Vector3(0,speed * Time.deltaTime ));
    3.     transform.Translate(new Vector3(speed * Time.deltaTime * a, speed * Time.deltaTime * a));
     
    Last edited: Jun 7, 2018
  5. SebastianLeeCN

    SebastianLeeCN

    Joined:
    Apr 26, 2018
    Posts:
    2
    Oh, I didn't notice that, thanks a lot.
     
    AlanMattano likes this.
  6. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Nice that I was helpful. :rolleyes: Looks like playing for a lot helps!
    A lot of times playing multiplayer games I use to run diagonally because you cover more territory. Especially jumping diagonally you cover more distance and so you can access to hiding places. ;)
     
    Tset_Tsyung likes this.
  7. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    I used that trick playing Goldeneye on the N64 against my brother. Because of that it was one of the few games I could actually beat him at!
     
    Last edited: Jun 13, 2018
    AlanMattano likes this.