i wrote this script for android device
so swapping right , left , jump and down
using UnityEngine;
using System.Collections;
public class BallMovement : MonoBehaviour {
public float minswipedisty ;
public float minswipedistx ;
private Vector2 startpos ;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if ( Input.touchCount > 0 )
{
Touch touch = Input.touches[0] ;
switch ( touch.phase )
{
case TouchPhase.Began :
startpos = touch.position ;
break ;
case TouchPhase.Ended :
float swipe_dist_vertical = (new Vector3(0,touch.position.y , 0 ) - new Vector3(0,startpos.y , 0 )).magnitude ;
if ( swipe_dist_vertical > minswipedisty )
{
float Swipe_value = Mathf.Sign ( touch.position.y - startpos.y ) ;
if ( Swipe_value > 0 )
{
print ("Jump") ;
//transform.Translate(new Vector3(0,20,0) ) ;
}
else if ( Swipe_value < 0 )
{
print ("Down") ;
}
}
float Swipt_dist_Horizontal = (new Vector3 ( touch.position.x , 0 , 0 ) - new Vector3(startpos.x,0,0)).magnitude ;
if ( Swipt_dist_Horizontal > minswipedistx )
{
float Swipe_Value = Mathf.Sign(touch.position.x - startpos.x ) ;
if ( Swipe_Value > 0 )
{
print ( "right") ;
transform.Translate(new Vector3(20,0,0) ) ;
}
else if ( Swipe_Value < 0 )
{
print ("Left") ;
transform.Translate(new Vector3(-20,0,0) );
}
}
break ;
}
}
}
}
when i try to jump
it is jumping and go to right at same time
when i try to down
it is down and go to left
so what is missing here ?