Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Change 2D GameObject based on touch direction

Discussion in 'Scripting' started by moEJoe82, Apr 11, 2018.

  1. moEJoe82

    moEJoe82

    Joined:
    Jun 14, 2017
    Posts:
    10
    This is driving me crazy, how to change a 2D gameobject's direction based on the touch swipe's direction, without lefting the mouse up.

    Just like in "Go Plane" game, the little plane in always moving forward in a constant speed, but the rotation is controlled by the user based on the swipe direction, wihtout the need to lift up his fingre.

    I don't want the plane to move toward the touch position, and i don't want the player to touch in the upper-right corner of the screen in order for the plane to rotate to the upper-right direction.

    What i want is to be able to capture the change in swipe direction (without lifting-up) in order to rotate and change the direction of the plane with a curve!

    Any help is HIGHLY appreciated!
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    It's not completely clear if you are asking about handling touch or mouse input.

    In the case of touch, then touch delta position could be for you.

    In the case of mouse, then take a look at getaxis

    One of those should help you out.
     
    moEJoe82 likes this.
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  4. moEJoe82

    moEJoe82

    Joined:
    Jun 14, 2017
    Posts:
    10
    Believe me, I did! I always ended up with a behaviour different from what I am looking for. either the object following the mouse position, or you have to do a full circle swipe to rotate the object by 360 degrees.
     
  5. moEJoe82

    moEJoe82

    Joined:
    Jun 14, 2017
    Posts:
    10
    here is my current code:

    Code (CSharp):
    1.  
    2.  
    3. public static Vector2 PlayerDirection;
    4.  
    5.     void Start () {
    6.         PlayerDirection = Vector2.zero;
    7.         currentPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    8.     }
    9.  
    10.     private Vector3 currentPos;
    11.  
    12.     void Update () {
    13.         PlayerDirection = Camera.main.ScreenToWorldPoint (Input.mousePosition) - currentPos;
    14.         PlayerDirection = (PlayerDirection.magnitude > 1) ? PlayerDirection.normalized : PlayerDirection;
    15.         print (PlayerDirection);
    16.  
    17.         float rotateAmount = Vector3.Cross (PlayerDirection, transform.up).z;
    18.  
    19.         //Move player
    20.         transform.GetComponent<Rigidbody2D> ().angularVelocity = -rotateAmount * curveSpeed;
    21.         transform.GetComponent<Rigidbody2D> ().velocity = transform.up * flyingSpeed;
    22.         currentPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    23.     }
     
  6. moEJoe82

    moEJoe82

    Joined:
    Jun 14, 2017
    Posts:
    10
    I have tried Touch.DeltaPosition to calculate the swipe direction, but still it needs to swipe all the way uo the screen in order to change the game object rotation to face up!