- Home /
Swipe controls error "Cannot implicitly convert type 'UnityEngine.Touch' to 'Touch' "
I'm trying to make my character move up and down when the player swipes up and down on their mobile scree but I get the error in the title. Here's the code:`using UnityEngine; using System.Collections;
public class Touch : MonoBehaviour {
public float minSwipeDistY;
public float minSwipeDistX;
private Vector2 startPos;
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 swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
if (swipeDistVertical > minSwipeDistY)
{
float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
if (swipeValue > 0)//up swipe
//Jump ();
else if (swipeValue < 0)//down swipe
//Shrink ();
}
break;
}
}
}
} `
Comment
Best Answer
Answer by NoseKills · May 05, 2015 at 10:34 PM
Your own class is called Touch just like Unitys class that holds information about individual touches. The compiler thinks you are referring to your Touch class whereas Input.touches[] contains UnityEngine.Touch objects.
You have to rename your class or make an unambiguous reference whenever you refer to unity's touch class
if (Input.touchCount > 0)
{
UnityEngine.Touch touch = Input.touches[0];