Look at Without Smooth

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:

    public Transform target;

    void Update() {
        // Rotate the camera every frame so it keeps looking at the target
        transform.LookAt(target);

    }

I tried with this too, but i had the same problem:

    public Transform target;

    void Update() {


        Vector3 relativePos = target.position - transform.position;
        Quaternion rotation = Quaternion.LookRotation(relativePos);
        transform.rotation = rotation;
    }

Here is a gif with the issues:

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

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.

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.

Thank you both for the answer. I have tried with the LateUpdate() and works!.
Thank you so much!.

Try with this

var targetObj : GameObject;
var speed : int = 5;
function Update(){
var targetRotation = Quaternion.LookRotation(targetObj.transform.position - transform.position);
      
// Smoothly rotate towards the target point.
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);