Dash for 2d game
hi im trying to make a game and am currently struggling with the dash mechanic, and really need help.
thanks in advance!
here is my code so far:
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;
public float dashSpeed = 100f;
private float dashTime;
public float startDashTime;
private int direction;
// private Vector2 lastMoveDir;
void Start(){
rb = GetComponent<Rigidbody2D>();
dashTime = startDashTime;
}
void FixedUpdate(){
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
// float x = Input.GetAxis("Horizontal");
// float y = Input.GetAxis("Vertical");
// Vector2 dir = new Vector2(x, y);
}
void Update(){
float xRaw = Input.GetAxis("Horizontal");
float yRaw = Input.GetAxis("Vertical");
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if(isGrounded == true && Input.GetKeyDown(KeyCode.W)){
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if(Input.GetKey(KeyCode.W) && isJumping == true){
if(jumpTimeCounter > 0){
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}else{
isJumping = false;
}
}
if(Input.GetKeyUp(KeyCode.W)){
isJumping = false;
}
if(Input.GetKeyDown(KeyCode.Space)){
if (direction == 0){
if (Input.GetKeyDown(KeyCode.A)){
direction = 1;
Debug.Log("left");
}else if (Input.GetKeyDown(KeyCode.D)){
direction = 2;
Debug.Log("right");
}
}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;
}
}
}
}
}
}
my movement is fine, but the dash...smh it just wont work. btw, I'm going for a celeste feel to this movement. thanks!
Comment
well i was looking for like a short burst of speed, sorta like a small teleport to whichever direction I went in last
hollow knight, celeste, all good examples..and great games :)