- Home /
How to give player a "dash" ability?
I'm making a 2D isometric/top down kind of game.
My character has this movement script attached:
public class PlayerController : MonoBehaviour {
public float moveSpeed = 5f;
public Vector2 lastMove;
private Rigidbody2D myRigidbody;
public bool canMoove; //used by scripts to halt player movement
void Start () {
myRigidbody = GetComponent <Rigidbody2D> ();
canMoove = true;
}
void FixedUpdate () {
if (canMoove == true) {
if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f) {
myRigidbody.velocity = new Vector2 (Input.GetAxisRaw ("Horizontal") * moveSpeed, myRigidbody.velocity.y);
playerMoving = true;
lastMove = new Vector2 (Input.GetAxisRaw ("Horizontal"), 0f);
} //movement on the horizontal axis
if (Input.GetAxisRaw ("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f) {
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, Input.GetAxisRaw ("Vertical") * moveSpeed);
playerMoving = true;
lastMove = new Vector2 (0f, Input.GetAxisRaw ("Vertical"));
} //movement on the vertical axis
if(Input.GetAxisRaw ("Horizontal") < 0.5f && (Input.GetAxisRaw ("Horizontal") > -0.5f))
{
myRigidbody.velocity = new Vector2 (0f, myRigidbody.velocity.y);
} //stops ice movement effect
if (Input.GetAxisRaw ("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
{
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, 0f);
} //stops ice movement effect
} else {
myRigidbody.velocity = new Vector2 (0f, 0f);
}
}
I would like to give the player a "dash" ability similar to those in Hollow Knight and Hyper Light Drifter. Where while moving, the character dashes forward in the direction they're moving (including along the diagonal). Or if they aren't moving, they just dash in the direction that they're facing.
Any ideas about how I could go about this? I've tried several scripts but none of them allow for a diagonal dash - only strictly horizontal and vertical. Any help would be appreciated.
Your answer
Follow this Question
Related Questions
AI phyics movement doesnt work 1 Answer
How to check place on empty for placement polygon Rigidbody2d? 0 Answers
,Rotate a gameobject around another while being attracted by its gravity 1 Answer
How to boxcast where a dynamic rigidbody2D's box collider will be in the next frame? 1 Answer
In unity 2D c# how to rotate an object like geometry dash? 1 Answer