I’m trying to set a minimum drag distance for the user controls. Essentially I need this in order to differentiate between a drag from a tap. I’ve tried a method that I used in another game, but couldn’t get it to work.
Here is the the basis of my drag script:
public static event Action<Vector3> OnSwipeStarted;
public static event Action<Vector3> OnSwipe;
public static event Action<Vector3> OnSwipeEnded;
void Update()
{
//set minimum drag distance
//Calculate drag distance
//if (drag distance > minimum drag distance)
//This is a drag
if (Input.GetMouseButtonDown (0))
{
startPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
if (OnSwipeStarted != null)
OnSwipeStarted (startPosition);
}
else if (Input.GetMouseButton (0))
{
Vector3 currentPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector3 movement = startPosition - currentPosition;
if (OnSwipe != null)
OnSwipe (movement);
}
else if (Input.GetMouseButtonUp(0))
{
Vector3 currentPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector3 movement = startPosition - currentPosition;
if (OnSwipeEnded != null)
OnSwipeEnded (movement);
}
}
Here is the concept that I’m trying to include in the above script:
So I got it working (mostly) with a few tweaks. I’m quite new to programming and game dev so I’m not exactly sure what I did to get things working . The only thing that is not working is the “distanceForSwipe” variable. It seems to be somehow stuck on 2.5f. If I change it to something like 1, the “wasSwipe” still registers as false if the distance was < 2.5.
void Update()
{
if (Input.GetMouseButtonDown (0))
{
startPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Debug.Log("Mouse Start Position : " + startPos);
startPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
if (OnSwipeStarted != null)
OnSwipeStarted (startPosition);
}
else if (Input.GetMouseButton (0))
{
Vector3 currentPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector3 movement = startPosition - currentPosition;
if (OnSwipe != null)
OnSwipe (movement);
}
else if (Input.GetMouseButtonUp(0))
{
Vector3 endPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float distance = Vector3.Distance(startPos, endPos);
wasSwipe = distance >= distanceForSwipe;
Debug.Log("Mouse End Position : " + endPos);
Debug.Log("Mouse Distance : " + distance);
Debug.Log("Was Swipe : " + wasSwipe);
Vector3 currentPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector3 movement = startPosition - currentPosition;
if (wasSwipe == true)
{
if (OnSwipeEnded != null)
OnSwipeEnded (movement);
}
}
}
change the value of wasSwipe in the inspector, not in the code. If you want to change it only in code, then make it private otherwise Unity will be overriding the value at the moment.
Also, you don’t need most of your original code now. Not sure why you are using the events or where they also being used in your game.
I think the events are being used in another script (There is a function in another script that is used for “firing” that uses them). When I use your code, it wont fire so I’m assuming I need those events. I haven’t written most of this, just got it off of the asset store to learn from. Slowly picking apart everything and adding things here and there to see what I can learn from it