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

[SOVLED] Diagonally movement too fast

Discussion in 'Scripting' started by ProExCurator, Mar 23, 2020.

  1. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    Diagonally movmement is too fast:

    Code (CSharp):
    1.        animator.SetFloat("Horizontal", Input.GetAxis("Horizontal"));
    2.        animator.SetFloat("Vertical", Input.GetAxis("Vertical"));
    3. ...
    4.             horizontal = JPE.JoystickHor;
    5.             vertical = JPE.JoystickVer;
    6.             animator.SetFloat("Horizontal", JPE.JoystickHor);
    7.             animator.SetFloat("Vertical", JPE.JoystickVer);
    8.             Vector3 move = GetComponent<Rigidbody2D>().velocity = new Vector3(horizontal * speed, vertical * speed, 0);
    9.             move.Normalize();
    This doesn't work. How can I make it work?
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
  3. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,926
    Line 8 isn't doing what you think it is.
    move
    isn't a reference to rigidbody.velocity -- it's another copy. So, normalizing move later on has no effect on the velocity. Try rearranging the code like this:
    Code (CSharp):
    1. Vector3 move = new Vector3(horizontal * speed, vertical * speed, 0);
    2. move.Normalize();
    3. GetComponent<Rigidbody2D>().velocity = move;
     
    ProExCurator likes this.
  5. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    It's working but it's very very slow now and changing speed doesn't make any difference :/
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Show what your code looks like now
     
  7. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    GameController:
    Code (CSharp):
    1. ...
    2. public static float moveSpeed = 15f;
    3. ...
    PlayerController:
    Code (CSharp):
    1. speed = GameController.MoveSpeed;
    2. ...
    3. float horizontal = Input.GetAxis("Horizontal");
    4. float vertical = Input.GetAxis("Vertical");
    5. ...
    6. horizontal = JPE.JoystickHor;
    7.             vertical = JPE.JoystickVer;
    8.             animator.SetFloat("Horizontal", JPE.JoystickHor);
    9.             animator.SetFloat("Vertical", JPE.JoystickVer);
    10.             Vector3 move = new Vector3(horizontal * speed, vertical * speed, 0);
    11.             move.Normalize();
    12.             GetComponent<Rigidbody2D>().velocity = move;
    and this is all.
     
  8. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    Normalize is giving you a unit vector, so speed is having no effect
    Code (CSharp):
    1.  
    2. Vector3 move = new Vector3(horizontal, vertical, 0);
    3. move.Normalize();
    4. GetComponent<Rigidbody2D>().velocity = move * speed;
    5.  
     
    ProExCurator likes this.
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    That's what I suspected, you're multiplying your speed before you normalize it. Multiply move by speed after you normalize it, it should work fine.
     
    ProExCurator likes this.
  10. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    You're right. Thanks ;)

    This is correct code:
    Code (CSharp):
    1. Vector3 move = new Vector3(horizontal, vertical, 0);
    2.             move.Normalize();
    3.             GetComponent<Rigidbody2D>().velocity = move * speed;