- Home /
what is Wrong in my Script ( c#) ?
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 ?
Comment
I'm guessing if ( Swipt_dist_Horizontal > $$anonymous$$swipedistx ) is always returning true.
Answer by haim96 · Oct 02, 2014 at 01:23 PM
i think (but not sure) that it because you check against zero. i bet you can't swipe exactly horizontal or vertical swipe, right? so swipe_value always 0.
try to check swipe_value against 10 for example.
see what happen.
Your answer
![](https://koobas.hobune.stream/wayback/20220613164236im_/https://answers.unity.com/themes/thub/images/avi.jpg)