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. Dismiss Notice

RotateAround get just called once

Discussion in 'Scripting' started by MisterMushn, Aug 3, 2021.

  1. MisterMushn

    MisterMushn

    Joined:
    Apr 30, 2021
    Posts:
    4
    I wanted to rotate the Camera around an object when the player touches and moves his finger on screen.

    The problem is that it just rotate once.

    I removed some code so its easy to replicate.

    - if i click on screen and keep touching, the Debug gives me "pre" and "post", but just ONE rotation
    - if i keep touching, it gives me still pre and post, but no rotation
    - i tried transform.rotation *= Quaternion.Euler(0, rotation,0) , just to see if i can control the camera, which is the case, and it keeps rotating itself when holding the touch

    The following script is on the camera

    Code (CSharp):
    1.  
    2.  
    3. if(Input.touchCount == 1)
    4.         {
    5.             Debug.Log("pre");
    6.             transform.RotateAround(Vector3.zero, Vector3.up, 10f);
    7.             transform.LookAt(Vector3.zero);
    8.             Debug.Log("post");
    9.         }
    10.  
     
  2. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    very important to note here that transform.LookAt sets the orientation, in other words, it overwrites the current rotation to a new one where the local z direction points at the given position

    so.... your transform.RotateAround does nothing to the rotation itself, after all, you immediately overwrite its result

    Edit:
    just noticed that you do RotateAround and LookAt on the same point (Vector3.zero), so you probably dont mind that LookAt overwrites the orientation
    is the problem that your object stops moving?
     
    Last edited: Aug 3, 2021
  3. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    try this:

    Code (CSharp):
    1. if(Input.touchCount == 1)
    2. {
    3.    transform.RotateAround(Vector3.zero, Vector3.up, 1f);
    4.    Vector3 relativePos = Vector3.zero - transform.position;
    5.    Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);
    6.    transform.rotation = rotation;
    7. }
    the LookAt apparently also overwrites your position change, this code works around that by doing the same as LookAt but calculating and writing the rotation separately, see:
    https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
     
    MisterMushn likes this.
  4. MisterMushn

    MisterMushn

    Joined:
    Apr 30, 2021
    Posts:
    4
    This works great
    Thank you very much