Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Unable to move horizontally but vertically is working

Discussion in 'Scripting' started by unity_A8T4R91FfTuy0g, Nov 5, 2023.

  1. unity_A8T4R91FfTuy0g

    unity_A8T4R91FfTuy0g

    Joined:
    Oct 16, 2023
    Posts:
    3
    Hello guys Im trying to move my player with a joystick for a mobile game project.I want my player to move at full speed or not at all but with my code down below I cant move my character horizontally but vertically is working fine.


    Code (CSharp):
    1.  
    2. public class TudmanMovement : MonoBehaviour
    3. {
    4.    
    5.     public FloatingJoystick joystick;
    6.     public float runSpeed = 40f;
    7.     public Rigidbody2D myRigidbody;
    8.     public Vector2 move;
    9.    
    10.  
    11.     private void Start()
    12.     {
    13.         myRigidbody = GetComponent<Rigidbody2D>();
    14.         myRigidbody.velocity = Vector2.zero;
    15.        
    16.     }
    17.     private void Update()
    18.     {
    19.         move.x = joystick.Horizontal;
    20.         move.y = joystick.Vertical;
    21.        
    22.     }
    23.     public void FixedUpdate()
    24.     {
    25.         if (joystick.Horizontal >= .3f)
    26.         {
    27.             myRigidbody.velocity = Vector2.right * runSpeed;
    28.         }
    29.         else if (joystick.Horizontal <= -.3f)
    30.         {
    31.             myRigidbody.velocity = Vector2.left * runSpeed;
    32.         }
    33.         else
    34.         {
    35.             myRigidbody.velocity = Vector2.zero;
    36.         }
    37.         if (joystick.Vertical >= .3f)
    38.         {
    39.             myRigidbody.velocity = Vector2.up * runSpeed;
    40.         }
    41.         else if (joystick.Vertical <= -.3f)
    42.         {
    43.             myRigidbody.velocity = Vector2.down * runSpeed;
    44.         }
    45.         else
    46.         {
    47.             myRigidbody.velocity = Vector2.zero;
    48.         }
    49.     }
    50.  
    51. }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,105
    Do you know that just overwriting the velocity doesn't magically keep what was there before? Setting the velocity to move right then setting it to move up just means it moves up.

    That's no different than you having a field with a value of (10,0) then writing (0,20) to it and expecting it to be (10,20). It'll be (0,20).

    You should read your input and form a Vector2 and assign the velocity once. Modify the X for the horizontal only and Y for the vertical.

    Also, constantly reading/writing to a native component means it's taking trips to/from the engine for that value. Just read it once, modify it as much as you like then write it back once.
     
    unity_A8T4R91FfTuy0g likes this.
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,576
    When ever you set the velocity to Vector2.up or Vector2.down then you are setting the velocity x to 0. You are also setting the velocity to Vector2.zero whenever the player is not moving up or down.
     
    unity_A8T4R91FfTuy0g likes this.
  4. unity_A8T4R91FfTuy0g

    unity_A8T4R91FfTuy0g

    Joined:
    Oct 16, 2023
    Posts:
    3
    Thanks for responding guys I have worked towards it after 3-5 refactoring the code I found the solution.
    Here it is!
    Code (CSharp):
    1. public class TudmanMovement : MonoBehaviour
    2. {
    3.    
    4.     public FloatingJoystick joystick;
    5.     public float runSpeed = 40f;
    6.     public Rigidbody2D myRigidbody;
    7.     private void Start()
    8.     {
    9.         myRigidbody = GetComponent<Rigidbody2D>();
    10.         myRigidbody.velocity = Vector2.zero;
    11.        
    12.     }
    13.     public void FixedUpdate()
    14.     {
    15.         myRigidbody.velocity = joystick.Direction.normalized * runSpeed;
    16.     }
    17. }
     
    MelvMay likes this.
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,105
    Code (CSharp):
    1. myRigidbody.velocity = Vector2.zero;
    Note that this is completely redundant. It'll default to zero.

    Code (CSharp):
    1. public Rigidbody2D myRigidbody;
    Also, this should be private because you get it yourself and don't need to set it in the inspector nor have it serialized.
     
    unity_A8T4R91FfTuy0g likes this.
  6. unity_A8T4R91FfTuy0g

    unity_A8T4R91FfTuy0g

    Joined:
    Oct 16, 2023
    Posts:
    3
    Everything works fine now the main problem is gone but there is still a problem remains and that is when the game starts for the first time.The first touch is not works no joystick appears no direction can be given it just goes straight at bottom left position till you untouch it any ideas?
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,105
    Quite simply, it's yours or 3rd party code for your joystick so you should debug it to determine why it gives you those values, presumably (-1,-1). It's nothing to do with physics though so impossible to tell you.
     
    unity_A8T4R91FfTuy0g likes this.