- Home /
checking raycast after swipe detection
I have two scripts, one is for checking what direction a user swiped on the screen, the other is to check if a user clicked on an object.
// if user clicked on object
public class touchDetector : MonoBehaviour {
float range;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
RaycastHit2D hit = Physics2D.Raycast ( worldPoint, Vector2.zero);
if (hit.collider != null) {
if (hit.transform.gameObject.CompareTag("Player")) {
// swipe detection here?
}
}
}
}
}
And for my swipe detection I am using a script from SquidSquadStudios, his video explaining this script: https://www.youtube.com/watch?v=9ZIHx09GevI
using UnityEngine;
using System.Collections;
public class SwipeDetection : MonoBehaviour
{
public float minSwipeDistY;
public float minSwipeDistX;
private Vector2 startPos;
void Update()
{
//#if UNITY_ANDROID
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
}
else if (swipeValue < 0)
{
// down swipe
}
}
float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
if (swipeDistHorizontal > minSwipeDistX)
{
float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
if (swipeValue > 0)
{
// right swipe
}
else if (swipeValue < 0)
{
//left swipe
}
}
break;
}
}
}
}
What I need to do is check for swiping after I check if touching an object, then move that object according to the swipe direction, it was not so simple as to putting the swipe detection code inside if (hit.transform.gameObject.CompareTag("Player")) { }
How can I combine these to scripts, so that I first check if an object was touched, then which direction the swipe was, or the other way around, checking if a user hit an object while swiping?
Your answer
Follow this Question
Related Questions
Swipe individual gameobject in a scene 0 Answers
Swipe individual gameobjects in a scene 0 Answers
How to flick a game object using touch and raycasters ? 0 Answers
Moving a 3D object with 2D swipes 2 Answers
Multitouch GameObject slicing 0 Answers