This post has been wikified, any user with enough reputation can edit it.
Question by
Brahmasys_unity · Aug 28, 2015 at 08:14 AM ·
physicsjumpswipe
Swiping problem (jumping)
Hello Everyone, I'm new to unity and trying to make a mobile game. In that im trying to make the character jump whenever i swiped the screen. i will give my sample code. so that it could be much help.
foreach (Touch touch in Input.touches) { if (touch.phase == TouchPhase.Began) {
initialTouch = touch;
}
if (touch.phase == TouchPhase.Moved && !hasswipped)
{
float deltaX = initialTouch.position.x - touch.position.x;
float deltaY = initialTouch.position.y - touch.position.y;
distance = Mathf.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
bool swipedsideways = Mathf.Abs(deltaX) > Mathf.Abs(deltaY);
if (distance > 150f)
{
if (swipedsideways && deltaX > 0) // swipped left (i don't want to move left or right)
{
this.transform.Rotate(new Vector3(0, 0, 0));
}
if (swipedsideways && deltaX <= 0) // swipped right
{
this.transform.Rotate(new Vector3(0, 0, 0));
}
if (swipedsideways && deltaY > 0) // swipped down
{
this.transform.Rotate(new Vector3(0, 0, 0));
}
if (swipedsideways && deltaY <= 0)
{
/* rb.velocity = new Vector3(rb.velocity.x, 0 , rb.velocity.z);
rb.AddForce(new Vector3(0, 100f, 0)); */
this.transform.Translate(Vector3.up * 100f * speed * Time.deltaTime);
// (i have tried the both ways like Rigidbody or normal transform) }
}
hasswipped = true;
}
else if (touch.phase == TouchPhase.Ended)
{
initialTouch = new Touch();
hasswipped = false;
}
and i have set my character to (rigidbody and capsule) physics and for ground also added rigidbody. thanks in advance.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612001730im_/https://answers.unity.com/themes/thub/images/avi.jpg)