Unity Player bumps when falls and dash
Hello, I made a dash for my game, but when it dashes while falling it wont directly dash to the right or lleft, it will go up depending on how fast player was falling and dash.When Dashing during jump seems no problems at all. I made a video(https://www.youtube.com/watch?v=t7d2v65wYwo&ab_channel=V.D.), on 3rd attempt of fall dash on it, there you can clearly see my bug. Here is the whole code for Player Movement
public class PlayerController : MonoBehaviour
{
[Header("Player Speed")]
public float runSpeed;
private Rigidbody2D rb;
private float horitonalMove;
public static bool facingRight;
[Header("Jump")]
public float jumpForce;
private float jumpTime;
public float jumpHoldTime;
private bool isJumping;
public int jumpCount;
private int currentJumpCount;
public float fallMultiplier;
[Header("Jump Checks")]
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool isGrounded;
[Header("Dash")]
public float dashForce;
public float dashTimer;
private bool isDashing;
private float currentDashTimer;
private float initialGravity;
void Start()
{
currentDashTimer = dashTimer;
rb = gameObject.GetComponent<Rigidbody2D>();
facingRight = true;
initialGravity = rb.gravityScale;
}
void Update()
{
horitonalMove = Input.GetAxisRaw("Horizontal");
if (Input.GetKeyDown(KeyCode.Space) && currentJumpCount > 0)
{
isJumping = true;
Jump();
}
if (Input.GetKey(KeyCode.Space) && isJumping == true)
{
JumpHigher();
}
else
{
jumpTime = jumpHoldTime;
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
Move();
if (isDashing)
{
Debug.Log(rb.velocity);
rb.velocity = new Vector2(rb.velocity.x, 0f);
Dash();
}
if (Input.GetKeyDown(KeyCode.E))
{
rb.velocity = Vector2.zero;
Debug.Log(rb.velocity);
isDashing = true;
currentDashTimer = dashTimer;
}
DashGravityController();
FlipController();
SetCameraOffset();
JumpController();
}
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
public void FlipController()
{
if (facingRight == true && horitonalMove < 0)
{
Flip();
}
else if (facingRight == false && horitonalMove > 0)
{
Flip();
}
}
public void Flip()
{
facingRight = !facingRight;
Vector2 Scaler = transform.localScale;
if (facingRight == false)
{
Scaler.x = -1;
}
else
{
Scaler.x = 1;
}
transform.localScale = Scaler;
}
public void Jump()
{
rb.velocity = Vector2.up * jumpForce;
currentJumpCount--;
}
private void JumpHigher()
{
if (jumpTime > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTime -= Time.deltaTime;
}
else if (jumpTime <= 0)
{
isJumping = false;
}
}
private void JumpController()
{
if (isGrounded == true && isJumping == false)
{
currentJumpCount = jumpCount;
}
if (isDashing == false && isJumping == false && rb.velocity.y < 0)
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}
}
private void Move()
{
rb.velocity = new Vector2(horitonalMove * runSpeed, rb.velocity.y);
}
public void Dash()
{
if (currentDashTimer <= 0)
{
isDashing = false;
}
else if (facingRight)
{
rb.velocity = Vector2.right * dashForce;
}
else if (!facingRight)
{
rb.velocity = Vector2.left * dashForce;
}
currentDashTimer -= Time.deltaTime;
}
public void DashGravityController()
{
if (isDashing)
{
rb.gravityScale = 0;
}
else
{
rb.gravityScale = initialGravity;
}
}
public void SetCameraOffset()
{
if (horitonalMove > 0)
{
CameraFollow.goingRight = true;
}
else if (horitonalMove < 0)
{
CameraFollow.goingRight = false;
}
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
I have already tried a lot of things but nothing worked. Like moving dash to FixedUpdate, setting gravity to 0 before dash, even freezing y axis.
Your answer
Follow this Question
Related Questions
Creating a Trampoline type object? 1 Answer
Why is my momentum not carrying over to the X axis? 0 Answers
How do I make a 2D player controlled ball roll when moving? 1 Answer
[Solved] Zero out Velocity before double jumping? 0 Answers
Bounce not working when Velocity is below 1f , what could it be? 0 Answers