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

Question Camera touch rotation Pokemon Go style

Discussion in 'Scripting' started by bogdanseb01, Feb 24, 2023.

  1. bogdanseb01

    bogdanseb01

    Joined:
    Sep 1, 2020
    Posts:
    2
    Hello everyone!

    Recently I have been trying to get my camera to rotate the same way it does in Pokemon Go and by that I mean whenever the player does a circular touch gesture(like the below picture) the camera rotates that way too and if they reach back to the original point where they started, the rotation keeps going. Also the ability to rotate like this both to the right and the left is something I have yet to replicate.

    Code (CSharp):
    1.  if(Input.touchCount==1 && inputsPaused == false)
    2.         {
    3.  
    4.  
    5.             Touch firstTouch = Input.GetTouch(0);
    6.  
    7.  
    8.  
    9.             //if (firstTouch.deltaPosition.y > 0)
    10.             //{
    11.             //    if (firstTouch.deltaPosition.x < 0)
    12.             //    {
    13.             //        transform.eulerAngles += new Vector3(0, 0, -40f * Time.deltaTime);
    14.             //        compas.Rotate(new Vector3(0, 0, -4f * Time.deltaTime));
    15.             //    }
    16.             //    else
    17.             //         if (firstTouch.deltaPosition.x > 0)
    18.             //    {
    19.             //        transform.eulerAngles += new Vector3(0, 0, 40f * Time.deltaTime);
    20.             //        compas.Rotate(new Vector3(0, 0, -4f * Time.deltaTime));
    21.             //    }
    22.  
    23.             //}
    24.             //else
    25.             //    if (firstTouch.deltaPosition.y < 0)
    26.             //{
    27.             //    if (firstTouch.deltaPosition.x < 0)
    28.             //    {
    29.             //        transform.eulerAngles += new Vector3(0, 0, 40f * Time.deltaTime);
    30.             //        compas.Rotate(new Vector3(0, 0, -4f * Time.deltaTime));
    31.             //    }
    32.             //    else
    33.             //         if (firstTouch.deltaPosition.x > 0)
    34.             //    {
    35.             //        transform.eulerAngles += new Vector3(0, 0, -40f * Time.deltaTime);
    36.             //        compas.Rotate(new Vector3(0, 0, -4f * Time.deltaTime));
    37.             //    }
    38.             //}
    39.  
    40.             if (firstTouch.position.y > 0)
    41.             {
    42.                
    43.                     transform.eulerAngles += new Vector3(0, 0, -40f * Time.deltaTime);
    44.                     compas.Rotate(new Vector3(0, 0, -4f * Time.deltaTime));
    45.              
    46.  
    47.             }
    48.             else
    49.                    if (firstTouch.position.y < 0)
    50.             {
    51.                
    52.                
    53.                     transform.eulerAngles += new Vector3(0, 0, 40f * Time.deltaTime);
    54.                     compas.Rotate(new Vector3(0, 0, -4f * Time.deltaTime));
    55.                
    56.              
    57.             }
    58.  
    59.  
    60.  
    61.             text.text = "X: " + firstTouch.position.x + " Y: " + firstTouch.position.y;
    62.  
    63.  
    64.  
    65.             //if (Input.GetTouch(0).phase == TouchPhase.Began)
    66.             //{
    67.             //    startAngle = Mathf.Atan2(touchPos.y, touchPos.x) * Mathf.Rad2Deg;
    68.             //    if (startAngle <= 0)
    69.             //    {
    70.             //        startAngle += 360;
    71.             //    }
    72.             //}
    73.             //angle = Mathf.Atan2(touchPos.y, touchPos.x) * Mathf.Rad2Deg;
    74.             //if (angle < 0)
    75.             //    angle += 360;
    76.             //distanceAngle = startAngle - endAngle;
    77.             //angle -= distanceAngle;
    78.             //if (angle < 0)
    79.             //    angle += 360;
    80.             //else if (angle > 360)
    81.             //    angle -= 360;
    82.             //if (Input.GetTouch(0).phase == TouchPhase.Ended)
    83.             //{
    84.             //    endAngle = angle;
    85.             //}
    86.  
    87.             //transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, angle);
    88.  
    89.         }
    90.  
    91.      
    92.  
    93.  
    94.  
    95.  
    96.  
    97.  
    98.  
    99.     }
    I have tried various code pieces but to no avail. I am trying to get my camera to rotate only on the Z axis, as I need my X rotation as 90* and Y as 0.

    If anybody has a clue on how to do this I would appreciate it :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    There's even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/

    If you want to do it yourself, definitely get away from any use of the .eulerAngles property, especially additive uses. That will almost certainly lead to undesirable results. Here's why:

    Notes on clamping camera rotation and NOT using .eulerAngles because of gimbal lock:

    https://forum.unity.com/threads/implement-a-limit-in-camera-movement.1100038/#post-7082380
    https://forum.unity.com/threads/implement-a-limit-in-camera-movement.1100038/#post-7083097

    How to instantly see gimbal lock for yourself:

    https://forum.unity.com/threads/confusing-bug.1165789/#post-7471441

    All about Euler angles and rotations, by StarManta:

    https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html

    You will always have better luck by:

    1. setting the camera position (transform.position = ...)
    2. telling the camera to look at another position (transform.LookAt( ...))
     
  3. bogdanseb01

    bogdanseb01

    Joined:
    Sep 1, 2020
    Posts:
    2
    Thank you for taking the time to compile all this information! It has been very helpful, the last thing that I am stuck on now is figuring out how to keep rotating to the direction determined by the circular touch gesture :(
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,903
    You need to keep a short list of the last n places that your touch has registered each frame. Every frame you're touching, throw out the oldest and add the newest. Then you can examine them when the touch ends, and extrapolate. Let's say that the last point is H, and the point before that is G. The next point in a line is H + (H-G). You can figure out the next point along a curve from F->G->H->? the same way.