- Home /
Unity 2d, Need help with Player Sliding when Swiping Down
I am trying to recreate my brother's project, I'm a completely new to Unity and programming, I watched a few tutorials to change my brother's key controls to touch, it worked till jump but after jump the game does not work the way intended, i have Slide as true as parameters for Run->Slide and for Slide->Run I just use exit time, in this scenario the player infinitely slides when i swipe down, when I swipe up, slide animation plays and at the end when he is about to touch the ground his jump animation plays, when I swipe down for a second time, he runs normally. When I give parameters for Slide->Run his movement is completely wrong, he uses a random frame for slide and no animation occurs when swiped down and he infinitely slides, when I swipe up, his slide animation comes with jumpforce, now I'm certain there is a problem with the code, any help please. I would really like to complete this project, thank you for your time.
void Update() {
grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
if (grounded)
myAnimator.SetFloat("Speed", myRigidbody.velocity.x);
myAnimator.SetBool("Grounded", grounded);
{
myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
}
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fingerStart = touch.position;
fingerEnd = touch.position;
}
if (touch.phase == TouchPhase.Moved)
{
fingerEnd = touch.position;
if (Mathf.Abs(fingerEnd.y - fingerStart.y) > 250)
{
if (fingerEnd.y - fingerStart.y > 250)//up swipe
{
if (grounded)
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
}
}
if (Mathf.Abs(fingerEnd.y - fingerStart.y) > -250)
{
if (fingerEnd.y - fingerStart.y > -250 && !sliding)//down swipe
{
sliding = true;
myAnimator.SetBool("Slide", true);
}
if (sliding && !(fingerEnd.y - fingerStart.y > -250))//up swipe
{
myAnimator.SetBool("Slide", false);
sliding = false;
}
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Scale up/down animator not staying at the scale once the animation finishes 2 Answers
Changing how my character looks depending on his current lifes. 2 Answers
Animator not working properly 1 Answer
Animator Trigger Not Working 1 Answer
How to play an animation when a slider is at a certain value. 1 Answer