Moving objects on Mouse swipe is a very interesting way of getting user input , it is very different from traditional
approach of getting input from keyboard keys . It provids user with unique experience and makes user part of the game.
Below is the code for getting Mouse swipe in Left , Right , Up , Down direction .
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour
{
private Vector2 initialPos ;
void Update ()
{
if( Input.GetMouseButtonDown(0) )
{
initialPos = Input.mousePosition;
}
if( Input.GetMouseButtonUp(0))
{
Calculate(Input.mousePosition);
}
}
void Calculate(Vector3 finalPos)
{
float disX = Mathf.Abs(initialPos.x - finalPos.x);
float disY = Mathf.Abs(initialPos.y - finalPos.y);
if(disX>0 || disY>0)
{
if (disX > disY)
{
if (initialPos.x > finalPos.x)
{
Debug.Log("Left");
}
else
{
Debug.Log("Right");
}
}
else
{
if (initialPos.y > finalPos.y )
{
Debug.Log("Down");
}
else
{
Debug.Log("Up");
}
}
}
}
}
0 Comment(s)