- Home /
2D Character won't jump diagonally
So I've been making a character controller with the jump and dash mechanic, both work fine, but when I try and make the character jump while dashing (and maintaining the velocity along the horizontal axis), the character simply jumps upwards, with no forward velocity, here's the jump code involved:
if (Input.GetButtonDown("Jump") && groundCheck) { if (Dashing == true) {
if (facingRight)
{
Debug.Log("DASH JUMP");
rb2d.velocity = new Vector2(rb2d.velocity.x + dashSpeed * 1, rb2d.velocity.y + jumpTakeOffSpeed);
}
else
rb2d.velocity = new Vector2(rb2d.velocity.x + dashSpeed * -1, rb2d.velocity.y + jumpTakeOffSpeed);
}
else
rb2d.velocity = new Vector2(rb2d.velocity.x, rb2d.velocity.y + jumpTakeOffSpeed);
timeStamp = Time.time;
canMove = true;
}
And here is the code for the dash:
if (groundCheck && Input.GetButtonDown ("Dash"))
{
Dashing = true;
timeStamp = Time.time + dashtime;
if (facingRight)
rb2d.velocity = new Vector2(dashSpeed * 1, 0);
else
rb2d.velocity = new Vector2(dashSpeed * -1, 0);
canMove = false;
}
if (Dashing && timeStamp <= Time.time)
{
Dashing = false;
canMove = true;
}
Simply walking and then pressing the jump button works just fine and the character will jump diagonally there, this is only a problem when I try to jump during the dash.
Is there any reason for that character to come to a stop complete stop, and then jump upwards instead of jumping diagonally?
P.S. "canMove" and "timeStamp" are related to the dash, the first is to stop the player from messing with the velocity using the directional buttons, and timeStamp is there to time the dash.
I'm trying to see if there's a variable or conditional that I forgot about that's forcing it to stop dashing before jumping, but so far there's nothing, I'll mention that if I find something.
@Viredae Could you get rid of dashSpeed * 1 and dashSpeed * -1
from your jump script and give it a try ?
Hey, sorry for not responding earlier, I tried removing those, no difference.
Answer by Maritto7 · Mar 17, 2018 at 06:06 AM
You are adding the dash speed twice and nullifying that as well. On the dash code try changing this line:
if (groundCheck && Input.GetButtonDown ("Dash"))
{
Dashing = true;
timeStamp = Time.time + dashtime;
if (facingRight)
rb2d.velocity = new Vector2((rb2d.velocity.x + dashSpeed), rb2d.velocity.y); //Here you were making Y speed and X speed 0 and then adding the dash speed.
else
rb2d.velocity = new Vector2((rb2d.velocity.x + dashSpeed) * -1, rb2d.velocity.y);//Here you were making Y speed and X speed 0 and then adding the dash speed.
canMove = false;
}
if (Dashing && timeStamp <= Time.time)
{
Dashing = false;
canMove = true;
}
Also, you should avoid setting velocity's directly, that can cause weird behaviors like stopping mid air and then regaining momentum. As you were doing. Try using the methods implemented by unity to prevent that.
Hey, thanks for responding. I'm kinda new to coding on Unity, so I'm not sure what methods you're talking about, I've been working from a lot of old tutorials because a lot of the official ones are pretty generic.
If there is something I love about unity is they have an awesome documentation. Here is a link for unity rigidbody methods and properties: https://docs.unity3d.com/ScriptReference/Rigidbody.html
And we have all been there, just read and practice a lot.
Your answer
Follow this Question
Related Questions
Having some stuck issues on the 2D infinite runner 0 Answers
Unity crashes 0 Answers