Question by
TheSorcerer · Dec 30, 2016 at 10:52 AM ·
scripting problemunity 2dtouch controlsswipeslide
Help with Slide when Swiping Down
Hey guys, I'm trying to recreate a previous project, completely new to unity, I'm updating the previous keyboard controls to touch controls, getting a problem when I add the slide script, this is my entire code for movement. Parameters for this animation is, from Run->Slide, Slide is true and for Slide->Run, Slide is false. Player slides infinitely and when I swipe up he completes his current slide animation and then while he is about to land he starts his jump animation. This is a Unity 2d endless runner.
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
myCollider = GetComponent<Collider2D>();
myAnimator = GetComponent<Animator>();
}
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)//down swipw
{
if (fingerEnd.y - fingerStart.y > -250 && !sliding)
{
sliding = true;
myAnimator.SetBool("Slide", true);
}
if (sliding && !(fingerEnd.y - fingerStart.y > -250))
{
myAnimator.SetBool("Slide", false);
sliding = false;
}
}
}
}
}
Comment