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.60k
    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 .

    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMove : MonoBehaviour
    5. {
    6. private Vector2 initialPos ;
    7. void Update ()
    8. {
    9. if( Input.GetMouseButtonDown(0) )
    10. {
    11. initialPos = Input.mousePosition;
    12. }
    13.  
    14. if( Input.GetMouseButtonUp(0))
    15. {
    16. Calculate(Input.mousePosition);
    17. }
    18.  
    19. }
    20.  
    21. void Calculate(Vector3 finalPos)
    22. {
    23. float disX = Mathf.Abs(initialPos.x - finalPos.x);
    24.  
    25. float disY = Mathf.Abs(initialPos.y - finalPos.y);
    26.  
    27. if(disX>0 || disY>0)
    28. {
    29. if (disX > disY)
    30. {
    31. if (initialPos.x > finalPos.x)
    32. {
    33. Debug.Log("Left");
    34. }
    35. else
    36. {
    37. Debug.Log("Right");
    38. }
    39. }
    40. else
    41. {
    42. if (initialPos.y > finalPos.y )
    43. {
    44. Debug.Log("Down");
    45. }
    46. else
    47. {
    48. Debug.Log("Up");
    49. }
    50. }
    51. }
    52. }
    53.  
    54. }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: