Hello. I have following code for handling player walking, jumping & dashing:
public class PlayerMovement : MonoBehaviour
{
[HideInInspector]public Rigidbody2D rb;
public float runSpeed = 5f;
public float jumpSpeed = 3f;
public Joystick joystickRun;
[HideInInspector]public float moveInputH;
[HideInInspector]public float moveInputV;
public float dashSpeed;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate() {
if(joystickRun.Horizontal >= .2f){
moveInputH = runSpeed;
transform.eulerAngles = new Vector3(0,0,0);
}
else if(joystickRun.Horizontal <= -.2f){
moveInputH = -runSpeed;
transform.eulerAngles = new Vector3(0,180,0);
}
else{
moveInputH = 0f;
}
rb.velocity = new Vector2(moveInputH, rb.velocity.y);
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Space)){
Jump();
}
if(Input.GetKeyDown(KeyCode.M) ){
Dash();
}
}
void Jump(){
rb.velocity = Vector2.up * jumpSpeed;
}
void Dash(){
rb.velocity = transform.right * (runSpeed * 6);
Debug.Log("Dash worked...");
}
}
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 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?