Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get mouse swipe input in Unity 3D?

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.58k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: