Search Unity

How do I used normalized my diagonal movement?

Discussion in 'Scripting' started by hizral, Feb 25, 2015.

  1. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    Hello there,

    as the tittle say, I'm having a bit of difficulties making my character diagonal speed the same speed as normal walking. It seems my diagonal speed is faster that normal walking. How do I fix this.

    Below Is my movement script :
    Code (CSharp):
    1. moveX = Input.GetAxis ("Horizontal") * (playerSpeed * Time.deltaTime);
    2.             moveY = Input.GetAxis ("Vertical") * (playerSpeed * Time.deltaTime);
    3.             mainCharacter.Translate (moveX , 0, moveY);
    Thank you,.
     
  2. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    Obviously it is faster. Walking 10 meters forward aint the same as moving 10 meters forward while at the same time moving 10 meters to the left or right. ;)
    What you are looking for is the normalized movement unit vector.
    http://docs.unity3d.com/ScriptReference/Vector3-normalized.html

    Code (CSharp):
    1. Vector3 movement = new Vector3( Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical")).normalized;
    2. mainCharacter.Translate ( movement * playerSpeed * Time.deltaTime );
     
    Guthree, amit-chai and Nanako like this.