- Home /
"Dash-Move" only work some times.
I have a problem with my dash move. It just work some times. Mby 1 out of 3 times. I also found out that it works more often if i press the jump key (SPACE) at the same time. Does it colide with the jump code? Any ideas how to fix it?
I marked the "dash code" with //Dash. I hope u can read the code im new in coding :D
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
private Rigidbody2D rb;
public float speed;
public float jumpForce;
private float moveInput;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
private bool facingRight = true;
//Dash
public float dashSpeed = 30f;
public float startDashTime = 0.1f;
private float dashTime;
private int direction;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
//Dash
dashTime = startDashTime;
}
private void FixedUpdate()
{
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
} else if(facingRight == true && moveInput < 0)
{
Flip();
}
void Flip()
{
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
//Dash
if (direction == 0)
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
if(moveInput < 0)
{
direction = 1;
} else if (moveInput > 0)
{
direction = 2;
}
}
} else
{
if(dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
rb.velocity = Vector2.zero;
} else
{
dashTime -= Time.deltaTime;
if(direction == 1)
{
rb.velocity = Vector2.left * dashSpeed;
} else if (direction == 2)
{
rb.velocity = Vector2.right * dashSpeed;
}
}
}
//Dash End
}
private void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if(isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if (Input.GetKey(KeyCode.Space) && isJumping == true)
{
if(jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
} else {
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
}
}
Thank you.
Answer by dougantor · Apr 05, 2020 at 10:03 AM
Player input is best handled in the update rather than fixedupdate, try moving everything in fixedupdate to update and that should help
Your answer
Follow this Question
Related Questions
Player getting stuck between grounds 0 Answers
OnCollisonEnter2D. 1 Answer
How to create soil or sand?? 1 Answer
Death Counter dont work when changing scenes 2 Answers
Having some stuck issues on the 2D infinite runner 0 Answers