Question by
omar.abouelleil · Feb 06, 2016 at 07:31 AM ·
errormouseswipesimple fix
What is wrong with this simple code?
I wanted a code that can detect a mouse click and swipe. I searched for that and found this one. All the comments were positive so I tried it out, but there were a few errors where it said. ; expected. ) expected. } expected
using UnityEngine;
using System.Collections;
public class DetectSwipe : MonoBehaviour {
//inside class
Vector2 firstPressPos;
Vector2 secondPressPos;
Vector2 currentSwipe;
public void Swipe()
{
if (Input.GetMouseButtonDown(0))
{
//save began touch 2d point
firstPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
}
if (Input.GetMouseButtonUp(0))
{
//save ended touch 2d point
secondPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
//create vector from the two points
currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
//normalize the 2d vector
currentSwipe.Normalize();
//swipe upwards
if (currentSwipe.y > 0 currentSwipe.x > -0.5f currentSwipe.x < 0.5f)
{
Debug.Log("up swipe");
}
//swipe down
if (currentSwipe.y < 0 currentSwipe.x > -0.5f currentSwipe.x < 0.5f)
{
Debug.Log("down swipe");
}
//swipe left
if (currentSwipe.x < 0 currentSwipe.y > -0.5f currentSwipe.y < 0.5f)
{
Debug.Log("left swipe");
}
//swipe right
if (currentSwipe.x > 0 currentSwipe.y > -0.5f currentSwipe.y < 0.5f)
{
Debug.Log("right swipe");
}
}
}
}
ps. I'm just a beginner and I don't really know that much coding
Comment
Best Answer
Answer by Jessespike · Feb 06, 2016 at 10:14 AM
The if-statements are missing AND operators (&&).
if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
if (currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
http://visualcsharptutorials.com/fundamentals/logical-operators/