- Home /
Can Dash only once 2D
Hello everyone I have implemented a dash mechanic in my 2D side scrolling project and I got it to work. The thing is it only works once. I think it is because the isDashing variable isn't going back to false. I am just having a hard time understanding why. If anyone could help me that would be amazing! Have a great day!
Kindly,
Harpoaian
here is my dash function void Dash() {
if (onGround == true && Input.GetKey(KeyCode.F) && isDashing == false && thePlayer.DashTimer() <= 1.0f)
{
float dashDirection = Input.GetAxis("Horizontal");
float dashSpeed = 500;
thePlayer.UpdateDashTimer();
GetComponent<Rigidbody2D>().velocity = new Vector2(thePlayer.MaxDashSpeed() * dashDirection, GetComponent<Rigidbody2D>().velocity.y);
isDashing = true;
Debug.Log("I am dashing!");
Debug.Log("DashTime = " + thePlayer.DashTimer());
Debug.Log(isDashing);
rb.AddForce(transform.right * dashSpeed);
}
else if(isDashing == true)
{
isDashing = false;
Debug.Log(isDashing);
}
}
Answer by MattG54321 · Jul 21, 2017 at 01:44 PM
Without seeing the rest of your code, I'm not exactly sure why your method isn't working. However, here's a potentially simpler way to do it:
public float dashCooldown = 1f; //How long the player has to wait before they can dash again
private float lastDash = 0f; //Amount of time since we last dashed
void Update () {
checkDash();
}
private void checkDash() {
lastDash += Time.deltaTime;
//If we've waited long enough and the player presses the dash key
if (lastDash >= dashCooldown && Input.GetKeyDown(KeyCode.F)) {
lastDash = 0f;
//I've pasted some of your dash code here:
float dashDirection = Input.GetAxis("Horizontal");
float dashSpeed = 500;
GetComponent<Rigidbody2D>().velocity = new Vector2(thePlayer.MaxDashSpeed() * dashDirection, GetComponent<Rigidbody2D>().velocity.y);
rb.AddForce(transform.right * dashSpeed);
}
}
I've done away with isDashing
, because we can just check the timer to see if we can dash again. Also, I recommend making dashSpeed
public and outside of any methods, so you can easily change it in the inspector.
I'm at work, so this is untested code. Feel free to come back with any questions. Good luck!
Thank you! Now the only thing I need is to make it stop after one second. I want this dash to function like in the $$anonymous$$ega $$anonymous$$an X series. At the current moment my player controller just keeps going when I am holding the F key. I want the player to dash for 1 second and then the timer reset back to zero. I don't want the player to be able to hold the dash forever XD. How should I approach that? Also thank you again good sir!
Alright I figured it out! I just had to change the Input.Get$$anonymous$$ey to Input.Get$$anonymous$$eyDown. Thank you very much :) You have helped me out a lot.
I tried this code but for some reason $$anonymous$$axDashSpeed keeps giving me a compiler error.
Assets\Scripts\Dash2.cs(30,74): error CS1061: 'object' does not contain a definition for '$$anonymous$$axDashSpeed' and no accessible extension method '$$anonymous$$axDashSpeed' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
Can you please help? I'm new to coding and I am still learning.
Answer by theonciest · Jan 19, 2018 at 03:12 AM
@MattG54321 Hi there, is there any way to convert this to 3d?
I feel it is this portion here -
// detect input movement
var moveHorizontal = Input.GetAxis("Horizontal");
var moveVertical = Input.GetAxis("Vertical");
IsMoving = moveHorizontal != 0 || moveVertical != 0;
IsRunning = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
if(IsRunning)
{
speed = runSpeed;
}
If there is any way you can help it would be greatly appreciated man. Been at this for days now. I've changed a few 2D scripts, and one really helped me understand it a bit more, but actually implementing it is impossible with my knowledge.
Thanks for you time
Your answer
Follow this Question
Related Questions
I need help with my dash 0 Answers
Unity 2D platformer collider 1 Answer