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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Top-down: Diagonal Axis looks faster than it should

Discussion in '2D' started by AndressMartin, May 17, 2018.

  1. AndressMartin

    AndressMartin

    Joined:
    Jun 24, 2017
    Posts:
    15
    I have this top-down 2D game & a very simple movement script:

    Code (CSharp):
    1.  private void Update()
    2. {
    3. Vector2 targetVelocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    4. playerRigidBody.velocity = targetVelocity * playerSpeed;
    5. }
    (playerSpeed is default 4f, playerRigidBody declared in void Awake())

    It works perfectly fine, however, when I am moving diagonal, the character *looks* faster. As the playerSpeed is 4, I am guessing that, when he is moving diagonals, the playerSpeed is 8 because of x & y combined, both with a value of 4.

    So, I decreased the speed to 2 when moving diagonals with another simple script that I can post in a comment if necessary.

    But the player looks SO slow now.

    How do most games get around this? Should I try the value of 3?

    I am sure I could find a number that makes the movement "look right," but I don't want to throw around numbers without any guidance, as I want the movement to be consistent.

    Thanks.
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    Normalize the vector by adding
    .normalized
    on the end.

    Code (csharp):
    1. Vector2 targetVelocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized;
     
  3. AndressMartin

    AndressMartin

    Joined:
    Jun 24, 2017
    Posts:
    15
    Jesus Newell, that worked, I didn't know there was a property for that, thanks a lot!