Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

transform.right doesn't work, but transform.up works for Dash script

Discussion in 'Scripting' started by goodgamershow, Apr 6, 2021.

  1. goodgamershow

    goodgamershow

    Joined:
    Apr 5, 2021
    Posts:
    15
    Hello. I have following code for handling player walking, jumping & dashing:

    Code (CSharp):
    1.  
    2. public class PlayerMovement : MonoBehaviour
    3. {
    4.    [HideInInspector]public Rigidbody2D rb;
    5.    public float runSpeed = 5f;
    6.    public float jumpSpeed = 3f;
    7.    public Joystick joystickRun;
    8.  
    9.    [HideInInspector]public float moveInputH;
    10.    [HideInInspector]public float moveInputV;
    11.    public float dashSpeed;
    12.  
    13.     void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.     }
    17.  
    18.     private void FixedUpdate() {
    19.         if(joystickRun.Horizontal >= .2f){
    20.             moveInputH = runSpeed;
    21.             transform.eulerAngles = new Vector3(0,0,0);          
    22.         }
    23.         else if(joystickRun.Horizontal <= -.2f){
    24.             moveInputH = -runSpeed;
    25.             transform.eulerAngles = new Vector3(0,180,0);      
    26.         }
    27.         else{
    28.             moveInputH = 0f;
    29.         }
    30.         rb.velocity = new Vector2(moveInputH, rb.velocity.y);
    31.     }
    32.     void Update()
    33.     {    
    34.         if(Input.GetKeyDown(KeyCode.Space)){
    35.            Jump();          
    36.        }
    37.  
    38.         if(Input.GetKeyDown(KeyCode.M) ){
    39.             Dash();          
    40.         }
    41.     }
    42.  
    43.     void Jump(){
    44.         rb.velocity = Vector2.up * jumpSpeed;
    45.     }
    46.     void Dash(){
    47.         rb.velocity = transform.right * (runSpeed * 6);
    48.         Debug.Log("Dash worked...");
    49.     }
    50. }
    The problem is that in Dash() method transform.right doesn't work at all, but if I replace it with transform.up, it works and Player dashes upwards. I tried using
    Code (CSharp):
    1. rb.AddForce(transform.right * dashSpeed * 400);
    Instead of transform.right, but the result is different -> Player literally teleports instead of dashing, that's why I stopped using it.
    Do you have any ideas, why transform.up works, but transform.right doesn't work?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Line 30 is ALWAYS setting the velocity, so Dash and Jump will do nothing (or almost nothing, perhaps just for a single frame).

    You are also mixing and matching doing physics in Update() and FixedUpdate(). It is a GOOD thing you are doing GetKeyDown() in Update() (in fact it is required!), but you should set flags that these inputs happened, then act upon them in FixedUpdate().
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    As Kurt is saying, your issue is you overwrite velocity all over the place instead of modifying velocity. On line 30 you are overwriting the X velocity with the contents of moveInputH, while keeping the Y velocity. So everywhere else you set velocity will always lose its X component.

    This is one reason Unity warns against setting velocity directly instead of just adding forces. You can easily end up in issues like this.