- Home /
Need help understanding advise
Hi everyone! I hope this will make sense.
I am trying to make a 2d character controller and i've already implemented basic 2d movement using "GetAxisRaw" and "rb.velocity = new Vector2 (moveInput * moveSpeed, rb.velocity.y);"
Now I am trying to add a dash feature like in Hollow Knight, and I found a great tutorial by Blackthornprod and he's doing it with "rb.velocity = Vector2.left/right * dashSpeed;". But this doesnt work for me and someone in the comments wrote "you are probably changing velocity of the rigidbody in your normal movement as well. make sure you are running this script after your movement script takes place so that this one takes control.", which I think is correct but I have no idea how to fix this. Unfortunately he's not answering so here we are.
What should I do? any help is much appreciated!
Answer by Magso · Feb 02, 2020 at 12:53 AM
You could use a bool with if statements.
if(dash){
rb.velocity = Vector2.left/right * dashSpeed;
}
else{
rb.velocity = Vector2.left/right;
}
Or set dashSpeed to an amount and have it -= by a deceleration value.
rb.velocity = Vector2.left/right * dashSpeed;
dashSpeed -= Time.deltaTime;
dashSpeed = Mathf.Clamp(dashSpeed, 1, //dash amount)
Thank you for replying! But I don't see how this is related the issue. $$anonymous$$y understanding was that I used rb.velocity wrong or not in the correct order. $$anonymous$$aybe I did not understand, could you please explain more?
"you are probably changing velocity of the rigidbody in your normal movement as well. make sure you are running this script after your movement script takes place so that this one takes control."
Basically if you have rb.velocity = ...
without any if condition in update, it will execute every frame i.e. override the movement.
Okay I think I get it. Currently my dash is an independent function which is called with a button press. my movement is currently inside FixedUpdate. So I need to condition my movement? if so, what would that condition be? Sorry for being slow, i'm still a beginner.
You would use a bool
to set the condition like in my first example above, dash
is the bool
. When the dash button is pressed start a coroutine and set dash to true. Yield the amount of time the dash should be true for and then set it back to false.
$$anonymous$$y second example doesn't need a coroutine. It is constantly applying the dash speed and the value is being decreased and clamped at 1. When the dash button is pressed, simply set dashSpeed
to an amount and it will be applied, decrease over time and the transition back to normal will be smoother.
Thank you for your help! After a lot of messing around i have finally got it working :)