- Home /
Need Help with MousePos and dashing
I'm a new unity user making a simple dash ability for my game. I'd like the player to dash towards the mouse on left click. I want to AddForce for the dash. However, all of the tutorials on this i've found either just dash in the direction of the last key pressed, or don't use force. I haven't tried anything because i'm really unfamiliar in this area. how would I go about doing this? if someone could explain how this works to me so I know how to do this in the future that would be great. Thanks! I do have this code I found on this website for a dash ability.
public class DashAbility : MonoBehaviour {
public DashState dashState;
public float dashTimer;
public float maxDash = 20f;
public Vector2 savedVelocity;
void Update ()
{
switch (dashState)
{
case DashState.Ready:
var isDashKeyDown = Input.GetKeyDown (KeyCode.LeftShift);
if(isDashKeyDown)
{
savedVelocity = rigidbody.velocity;
rigidbody.velocity = new Vector2(rigidbody.velocity.x * 3f, rigidbody.velocity.y);
dashState = DashState.Dashing;
}
break;
case DashState.Dashing:
dashTimer += Time.deltaTime * 3;
if(dashTimer >= maxDash)
{
dashTimer = maxDash;
rigidbody.velocity = savedVelocity;
dashState = DashState.Cooldown;
}
break;
case DashState.Cooldown:
dashTimer -= Time.deltaTime;
if(dashTimer <= 0)
{
dashTimer = 0;
dashState = DashState.Ready;
}
break;
}
}
}
public enum DashState
{
Ready,
Dashing,
Cooldown
}
If anyone could maybe help modify it to work for this that would also be epic. (the page where I got the code is here.)
Your answer
Follow this Question
Related Questions
Use Rigidbody2D's Drag to slow down flying character 1 Answer
How do I get the Vector2 from an input action in code? (New Input System) 1 Answer
Issue with mouse position script 1 Answer
AddForce knockback x axis according to mouse.position more then y axis (2D game) 0 Answers
How to give player a "dash" ability? 0 Answers