So I asked a similar question few days ago regarding jumping, and it lead me to an aswer that I’m actually cancelling out my Y velicity with the part of the script that handles rest of the movement. Now I was thinking about implementing dashing using rigidbody2D.AddForce(Vector3.right * dashSpeed); ,but the code is being cancelled out because the X value ir being updated every frame using values from directional keys, if that makes sense. So can you guys please help me figure out a workaround?
Here’s my code:
public int HorizontalSpeed = 10;
public float jumpSpeed = 1000f;
public float dashSpeed = 1000f;
private Vector2 movement;
public bool fallin = false;
void Update () {
//Horizontal movement
float inputX = Input.GetAxis ("Horizontal");
movement = new Vector2 (inputX * HorizontalSpeed, rigidbody2D.velocity.y);
}
void FixedUpdate () {
//Jumping
if (Input.GetKeyDown(KeyCode.X))
{
rigidbody2D.AddForce(Vector3.right * dashSpeed);
}
//Jumping
if (Input.GetKeyDown(KeyCode.Space) && fallin == false)
{
rigidbody2D.AddForce(Vector3.up * jumpSpeed);
}
fallin = true;
rigidbody2D.velocity = movement; //This zeroes out the x value each frame
// so I can't dash properly. It just blinks instead of being smooth. Workaround?
}
void OnCollisionStay2D() {
fallin = false;
}