- Home /
C# script problem. Dash ability
I'm Trying to make a dash ability like the game Celeste. If I press W/A/S/D and then shift the dash works, but If I press shift first it doesn't. Help? / / -Grounded code, attached to ground check / / using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Grounded : MonoBehaviour { GameObject Player; // Start is called before the first frame update void Start() { Player = gameObject.transform.parent.gameObject; }
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.tag == "Ground")
{
Player.GetComponent<PlayerMove>().isGrounded = true;
Player.GetComponent<Dash>().actDash = true;
Player.GetComponent<Dash>().Colliding = true;
Player.GetComponent<Dash>().isJumping = false;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.collider.tag == "Ground")
{
Player.GetComponent<PlayerMove>().isGrounded = false;
Player.GetComponent<Dash>().Colliding = false;
Player.GetComponent<Dash>().isJumping = true;
}
}
} / -PlayerMove code, attached to Player: / / -Dash code, attached to player: / / using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Dash : MonoBehaviour { private Rigidbody2D rb; public float dashSpeed; private float dashTime; public float startDashTime; private int direction; public bool actDash = true; public bool Colliding = true; public bool isJumping = true;
void Start()
{
rb = GetComponent<Rigidbody2D>();
dashTime = startDashTime;
}
void Update()
{
if (direction == 0)
{
if (actDash == true && Input.GetKeyDown(KeyCode.LeftShift))
{
if (Input.GetKey(KeyCode.A))
{
if (Colliding == true)
{
direction = 1;
}
else if(isJumping == true)
{
direction = 1;
actDash = false;
}
}
else if (Input.GetKey(KeyCode.D))
{
if (Colliding == true)
{
direction = 2;
}
else if (isJumping == true)
{
direction = 2;
actDash = false;
}
}
else if (Input.GetKey(KeyCode.W))
{
direction = 3;
actDash = false;
}
else if (Input.GetKey(KeyCode.S))
{
if (Colliding == false)
{
direction = 4;
actDash = false;
}
}
}
} 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;
}
else if (direction == 3)
{
rb.velocity = Vector2.up * dashSpeed;
}
else if (direction == 4)
{
rb.velocity = Vector2.down * dashSpeed;
}
}
}
}
}
Answer by xxmariofer · Jul 26, 2019 at 07:38 PM
change
Input.GetKeyDown(KeyCode.LeftShift)
to
Input.GetKey(KeyCode.LeftShift)