Hi Guys, I Have one problem.
I have 2 animation. Anim_Left and AnimRight.
So my question, this is the script I use in C#.
If I swipe to the left, the animation “Anim_Left” has to play, and if I swipe to the right, animation “Anim_Right” needs to play. Someone who can help me out with this problem?
Thanks in advance.
//C#
//SwipeHandler.cs
using UnityEngine;
public class SwipeControl : MonoBehaviour
{
public float minMovement = 20.0f;
public bool sendLeftMessage = true;
public bool sendRightMessage = true;
public GameObject MessageTarget = null;
private Vector2 StartPos;
private int SwipeID = -1;
void Update ()
{
if (MessageTarget == null)
MessageTarget = gameObject;
foreach (var T in Input.touches)
{
var P = T.position;
if (T.phase == TouchPhase.Began && SwipeID == -1)
{
SwipeID = T.fingerId;
StartPos = P;
}
else if (T.fingerId == SwipeID)
{
var delta = P - StartPos;
if (T.phase == TouchPhase.Moved && delta.magnitude > minMovement)
{
SwipeID = -1;
if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
{
if (sendRightMessage && delta.x > 0)
MessageTarget.SendMessage("OnSwipeRight", SendMessageOptions.DontRequireReceiver);
else if (sendLeftMessage && delta.x < 0)
MessageTarget.SendMessage("OnSwipeLeft", SendMessageOptions.DontRequireReceiver);
}
}
else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
SwipeID = -1;
}
}
}
void OnSwipeLeft()
{
transform.position -= Vector3.right*80;
}
void OnSwipeRight()
{
transform.position += Vector3.right*80;
}
}