Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Look at Without Smooth

Discussion in 'Scripting' started by Raultruji3D, Nov 3, 2017.

  1. Raultruji3D

    Raultruji3D

    Joined:
    Nov 3, 2017
    Posts:
    2
    Hi, i have a camera with a look at which in pc runs good but in the mobile it has some issues: the camera in some frames hasn't a correct look at, and it makes the object vibrate. I've detected that the look at has a smooth movement, and i think that that it is the problem.

    My code is:

    Code (CSharp):
    1.     public Transform target;
    2.  
    3.     void Update() {
    4.         // Rotate the camera every frame so it keeps looking at the target
    5.         transform.LookAt(target);
    6.  
    7.     }
    I tried with this too, but i had the same problem:

    Code (CSharp):
    1.     public Transform target;
    2.  
    3.     void Update() {
    4.  
    5.  
    6.         Vector3 relativePos = target.position - transform.position;
    7.         Quaternion rotation = Quaternion.LookRotation(relativePos);
    8.         transform.rotation = rotation;
    9.     }
    Here is a gif with the issues:

    https://i.makeagif.com/media/11-03-2017/UlFIoF.gif
     
  2. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    There doesn't seem to be anything wrong with the code. If I had to guess, it looks like on PC version the Update() execution sequence is target -> camera and on mobile it's camera -> target, which makes LookAt lag a frame behind.

    Try using LateUpdate() for LookAt.
    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    That has nothing to do with smoothing, but with "Update lag" or script execution order. You're adjusting the position of the target in another Update and you're thinking this is what happens:
    - Update position of target
    - Let the camera look at the target

    But what is actually happening is:
    - Let the camera look at the target
    - Update position of target
    And therefore the camera look at is always one frame behind.

    You can change the ordering of the Update calls using script execution order, but recommend changing the code. (Before the transform.LookAt call, call the script to update the target position.
     
  4. Raultruji3D

    Raultruji3D

    Joined:
    Nov 3, 2017
    Posts:
    2
    Thank you both for the answer. I have tried with the LateUpdate() and works!.
    Thank you so much!.
     
  5. Marce_23

    Marce_23

    Joined:
    Oct 10, 2017
    Posts:
    1
    Try with this
    Code (CSharp):
    1. var targetObj : GameObject;
    2. var speed : int = 5;
    3. function Update(){
    4. var targetRotation = Quaternion.LookRotation(targetObj.transform.position - transform.position);
    5.      
    6. // Smoothly rotate towards the target point.
    7. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);