- Home /
2D top down dash code
'Ive been looking around to get a good dash code for my character and i cant get my velocity to change hoping you could help here's my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DashAbility : MonoBehaviour {
public float dashCooldown;
public bool canIDash = true;
public Vector2 CurrentVel;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
CurrentVel = GetComponent<Rigidbody2D>().velocity;
if (dashCooldown > 0)
{
canIDash = false;
dashCooldown -= Time.deltaTime;
}
if (dashCooldown <= 0)
{
canIDash = true;
}
if (Input.GetKeyDown(KeyCode.LeftShift) && canIDash == true)
{
//this part is the actual dash itself
CurrentVel = new Vector2(CurrentVel.x * 10, CurrentVel.y * 10);
//sets up a cooldown so you have to wait to dash again
dashCooldown = 2;
}
}
}
Need to see whole code to help you, like how are you moving player?
Also please rename your CurrentVel as currentVel. $$anonymous$$ethods like Update and Start should be started with uppercase letter and varaibles like currentVel and dashCooldown with lowercase letter. It will work either way, but you can already see coloring being wrong (CurrentVel and Update have same color).
If you need canIDash in somewhere else than this then it's just fine, but if not then you don't need it at all. You could just simply check for the time like this:
void Update ()
{
currentVel = GetComponent<Rigidbody2D>().velocity;
dashCooldown -= Time.deltaTime;
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift) && dashCooldown <= 0)
{
//this part is the actual dash itself
currentVel = new Vector2(currentVel.x * 10, currentVel.y * 10);
//sets up a cooldown so you have to wait to dash again
dashCooldown = 2;
}
}
Oh and you should create new variable private Rigidbody2D rb2d; and then get it once in start rb = GetComponent(); as it's somewhat slow thing to do. After that you get velocity with just currentVel = rb2d.velocity;
Thanks for responding! This is my movement code.
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour
{
public float walkSpeed = 4f;
public float runSpeed = 8f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
Vector2 targetVelocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
GetComponent<Rigidbody2D>().velocity = targetVelocity * walkSpeed;
}
The problem might be that you dash, but, well, you reset the velocity in movement section so your dash velocity gets overrided.
Answer by ThisGuyThatsHere · Feb 20, 2017 at 09:49 AM
The problem is dashing gets overrided by movement, I guess you don't really want to play with curves etc. so let's keep it simple.
Instead of doing modifications in the ability which could be pretty advanced you should be fine with simple player movement rework:
public class PlayerMovement : MonoBehaviour
{
public float walkSpeed = 4f;
public float runSpeed = 8f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
Vector2 targetVelocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if(targetVelocity.magnitude() * walkspeed > GetComponent<Rigidbody2D>.velocity.magnitude()) GetComponent<Rigidbody2D>().velocity = targetVelocity * walkSpeed; //So, dash results in higher velocity than normal movement? then let's override it only if it got slower than movement!
}
}
This is far from perfect and you can obviously do that cpu cheaper way, but should work.
Your answer
![](https://koobas.hobune.stream/wayback/20220612111356im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can anyone tell me why this code isn't working? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
HOLD THE JUMP BUTTON TO JUMP HIGHER 0 Answers
Transferring velocity from kinematic to a rigid body 0 Answers