- Home /
Free 2d dash ability script!
I have a dashing mechanic in my game. I was using Transform, but since transform doesn't account for physics I was going through walls on occasion. I'm trying to use addforce, but it doesn't have the quick burst of speed effect as previously.
how can I use addforce to make the dash seem more like a dash?
I think my dash's cooldown is messed up too, allowing the player to spam the button, instead of using it once, then having to wait. If someone could check that out it would help me alot too.
if(Input.GetKey(KeyCode.LeftShift) && canIDash == true){
if(direction == true)
{
dashTimer += Time.deltaTime*3;
rigidbody.AddForce(Vector3.right*50);
}
if (direction == false)
{
dashTimer += Time.deltaTime*3;
rigidbody.AddForce(Vector3.left*50);
}
}
if(dashTimer > .5f)
{
canIDash = false;
dashCooldown = true;
}
if(dashTimer < .5f && dashCooldown == false)
{
canIDash = true;
}
if(dashTimer <= 0)
{
dashCooldown = false;
}
}
EDIT:
This is what the script currently looks like, it still doesn't work perfect. If anyone can help it would be awesome.
public class DashAbility : MonoBehaviour {
public float dashCooldown;
public bool canIDash = true;
public Vector2 savedVelocity;
void Update ()
{
if(dashCooldown > 0)
{
canIDash = false;
dashCooldown -= Time.deltaTime;
//if I put saved velocity here, it will continue to move at savedVelocity until dashCooldown = 0? so it can't go here? right?
}
if(dashCooldown <= 0)
{
canIDash = true;
//if I put savedVelocity here it doesn't return to savedVelocity until dashCooldown <=0 so... it doesn't go here either right...?
}
if(Input.GetKeyDown(KeyCode.LeftShift) && canIDash == true)
{
//saves velocity prior to dashing
savedVelocity = rigidbody.velocity;
//this part is the actual dash itself
rigidbody.velocity = new Vector2(rigidbody.velocity.x*3f, rigidbody.velocity.y);
//sets up a cooldown so you have to wait to dash again
dashCooldown = 2;
}
}
F=$$anonymous$$A. Changing force means changing acceleration, so it's not going to be an instantaneous speed change.
Have you tried changing the rigidbody.velocity
?
I dunno, doesn't seem to be really doin it.
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift) && canIDash == true){
if(direction == true)
{
dashTimer += Time.deltaTime*3;
rigidbody.velocity = new Vector2(25, 0);
}
if (direction == false)
{
dashTimer += Time.deltaTime*3;
rigidbody.velocity = new Vector2(-25, 0);
}
}
plus the velocity is maintained if you hold left shift and i'm not sure how to change that.
What do you mean by doesn't seem to be doing it?
What's the original velocity? If it's around 20 to 25, you won't see much difference. I'd expect a dash to be along the lines of rigidbody.velocity = rigidbody.velocity * 2;
(or additive, either way).
To stop it from maintaining the velocity, you'll need a way to set the velocity back when you're not holding shift. So when that dash timer runs out, for example, set the velocity back to a standard value.
If you don't have a static velocity in 'normal' (non-dash) mode, you'll want to preserve your previous velocity in a private variable just before you 'dash', then reset it back after.
Does that make sense?
BTW, you have your dash variables set up with dashTimer, dashCooldown, and canIDash. Yet it looks like canIDash is supposed to represent a fixed calculation of information based on the other two.
That situation is perfect for properties. Try this ins$$anonymous$$d:
private float dashTimer;
private bool isDashOnCooldown;
private bool canIDash
{
get { return dashTimer < .5f && isDashOnCooldown == false; }
}
Then you never have to set canIDash yourself.
Answer by Alanisaac · Feb 04, 2015 at 01:48 PM
Ok, I'm pulling this out of the comment thread. From a technical perspective, your dash mechanic may be modeled as a state machine.
We really don't need to get that complicated or formal in our implementation, but it might be helpful to think of dashing as a set of states (Ready, Dashing, Cooldown), with specific conditions that transition from state to state (is the dash key down?, is max dash hit?, is cooldown time over?).
Try out something like the following script. It uses an enum
to represent the three states. Notes:
Dash can only be activated when you're in the
Ready
state, and activating it sets theDashing
state. This prevents the dash button continuously attempting to save previous velocity.For the other two cases, I added a bit of value coercion so the timer never goes outside of 0 or the max.
Let me know if you have any questions!
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
}
I just made a slight change and decided to only save the x velocity ins$$anonymous$$d of the x and y velocity. this way when we return to savedVelocity the character doesn't continue moving up/down for a second(Unless you can think of a better way to stop that). Thank you very much for your help with this it works perfectly and exactly how I wanted it to!
I also changed the questions title so if anyone in the future needs this script they can find it easier :)
Any chance you can share how you saved just the x velocity?
Hi Alanisaac, I've tried your code with my project but it doesn't seem to do anything with my movement. I've created the needed .cs file and attached to the object (player) with the correct variables like the rigidbody and so on. I see the Dash State, Dash Timer and $$anonymous$$axDash in the inspector in Unity but when I test it out, it detects the button presses, changes to dashing, and when it reaches $$anonymous$$axDash it cools down to 0. Unfortunately it doesn't alter the movement in my character.
I could post my code but it is essentially a copy-paste of what you provided. I would be really glad... like... REALLY GLAD if you help me out. Dash is quite tricky sometimes. I attach a screenshot of what you would expect from my project and the values of the .cs of my PlayerController, as well as the physics of my Rigidbody 2D.
Thanks a lot in advance. I want to progress with my project :)
I litlebit corrected code and that code will work try to play with "dashForce" in Unity
public class ScriptName : MonoBehaviour
{
private Rigidbody2D rb;
public DashState dashState;
public float dashForce;
public float dashTimer;
public float maxDash = 20f;
public Vector2 savedVelocity;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
switch (dashState)
{
case DashState.Ready:
var isDashKeyDown = Input.GetKeyDown(KeyCode.LeftShift);
if (isDashKeyDown)
{
savedVelocity = rb.velocity;
rb.AddForce(new Vector2(rb.velocity.x * dashForce, rb.velocity.y));
dashState = DashState.Dashing;
}
break;
case DashState.Dashing:
dashTimer += Time.deltaTime * 3;
if (dashTimer >= maxDash)
{
dashTimer = maxDash;
rb.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
}
Very useful, helped me make my group project dash ability in school, would recommend to others looking for the same script, only note, makes the player character abruptly stop, without a smooth stopping phase, if there could be one implemented, that would be nice to know.
Your answer
Follow this Question
Related Questions
Unity 3d Elevator Script Not Working Properly 0 Answers
Making a boost for a spaceship with cooldown. 1 Answer
Setting a delay before dashing. 1 Answer
Cooldown/Timer system (Javascript) 0 Answers
.js Timer to prevent chatflood? 1 Answer