Camera follow

I have made a camera follow script:

    public Transform target;

    private Vector3 offset;

    private void Start()
    {
        offset = transform.position - target.position;
    }

    private void LateUpdate()
    {
        transform.position = Vector3.Lerp(transform.position, target.position + offset, 10f * Time.deltaTime);
    }

But my camera follow is lagging
ybenoe

Someone any idea how I can fix this?

Lerp is not really appropriate for this circumstance, nor are you using it correctly. Try Vector3.MoveTowards:

public Transform target;

// Change this in the Editor to modify the follow speed.
public float CameraMoveSpeed = 1f;

private Vector3 offset;

private void Start() {
    offset = transform.position - target.position;
}

private void LateUpdate() {
    var targetPosition = target.position + offset;
    transform.position = Vector3.MoveTowards(transform.position, targetPosition, CameraMoveSpeed * Time.deltaTime);
}

Hey, thank you for your answer! The player is still lagging. It had really fast teleport jumps it seems, just lag. So it didn’t fix it.

Sounds(and looks) like there’s probably a problem with your player move script then.

No because when the camera is not moving everything is fine.

Do you have any other scripts that are modifying the position of the camera? Is the camera a child object of anything in the scene?

No, the camera is only controlled by this script and just in the scene not as a child.

I’m not sure I understand what you mean by lag so would you mean posting another video of what it looks like with the new script?

With your new script it looks exactly the same as my video above.

I changed the lateupdate to fixedupdate and it fixed the problem. But now when I walk everything seems fine but I see a slight blur kind of thing on the zombie when I start moving, it is a very little shaky still. Any idea? It seems like the camera follow is not at full frame rate so it seems laggy a bit still.

Rigidbodies move in FixedUpdate, Rendering happens in Update. Maybe your player and your enemy are moving in different updates - so if the camera stands still, everything looks good, if it is moving, one or the other seems laggy.

You can fix this by interpolating your camera velocity, so the cam has its own movement speed.
This is from a 2D project, but the theory for 3D is the same:
transform = player, camTransform = camera transform - just to avoid confusions.
Players Rigidbody-Interpolate is set to “Interpolate”

private void LateUpdate()
        {
            if (!camTransform) return;
            MoveCam();
        }

        [SerializeField] float _interpVelocity = 0.5f;
        Vector3 vel;

        void MoveCam()
        {
            Vector3 posNoZ = camTransform.position;
            posNoZ.z = transform.position.z;

            Vector3 targetDirection = (transform.position - posNoZ);

            _interpVelocity = targetDirection.magnitude * 5f;

            var targetPos = camTransform.position +
                (targetDirection.normalized * _interpVelocity * Time.deltaTime);

            camTransform.position = Vector3.SmoothDamp(camTransform.position,
                targetPos, ref vel, camSpeed * Time.deltaTime);
        }

Yes, as @John_Leorid says, this problem comes from having the camera and the follow object in differents timestep updates. Also, if any other object (like your zombie) is in a different timestep than the camer, it will jitter if both are moving.
Using Rigidbody.Interpolate may do de trick, but you will need to use it on every Rigidbody that is jittering, and depending on your target platform it may lead to some bad performance. Also, moving your camera in FixedUpdate will result in a not as smoothy movement like it was in Update.

Have you considered using CharacterController component for movement instead of Rigidbody? It may be very handy if you are not developing a physics-driven game, and you can move it in Update and get rid of this mess with timesteps. Also, it has some nice parameters to control the max slope a character can walk, or the max height a character will “step up” (among other cool things). If you are interested in, take a look to this link Unity - Manual: Character Controller component reference

Hope to be helpful :smile:

When I use this script my camera goes slowly into the ground.

I want my player to collide with walls, zombies etc so I will need a rigidbody. But I am getting so anoyed by this lagging/blurring zombie. The zombie is using a navmeshagent.

I hope anyone has any idea how to fix it.

CharacterController handles collision, you can use it without needing any further Collider or Rigidbody. If you need a Rigidbody because you want to listen to OnCollisionEnter events, just tick rigibody’s IsKinematic to avoid any physic calculation like gravity or so on, since CharacerController will handle them too :smile:

With the charactercontroller the camera is lagging too, now the player again.

Even with using assets store camera follow scripts my zombie keeps blurring/lower framerate. Anyone any idea?

So, let’s put everything together.

  1. Move your player and enemies inside Update method (not FixedUpdate).
  2. Move your camera inside Update method (not FixedUpdate), or even better inside LateUpdate method.
  3. Use CharacterController for movement instead of Rigidbody, since Rigidbody should be moved inside FixedUpdate and now you want to move your character inside Update rather than FixedUpdate.
  4. If you still want to have a Rigidbody component attached to your object, make sure to tick te IsKinematic option in the Inspector since you are handling movement with CharacterController.

If after following this steps doesn´t work, please paste your code so I can check it out :smile:

Hope it helps!

Thank you, the problem was I was doing it in fixedupdate with the character controller! Now I’ll have to figure out how to make my character controller work properly because it sucks now.

Vector3 moveInput = new Vector3(horizontal, 0f, vertical);

GetComponent<CharacterController>().Move(moveInput * Time.deltaTime * movementSpeed);

I am using this now but that doesn’t work so well. You have any good tutorials for it?

// Walking
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");

        if (GetComponent<CharacterController>().isGrounded)
        {
            moveDirection = new Vector3(horizontal, 0f, vertical);
            moveDirection *= movementSpeed;
        }

        moveDirection.y -= gravity;

        GetComponent<CharacterController>().Move(moveDirection * Time.deltaTime);

I am using this to move, but it is very weird. In some directions my player moves slowly, randomly speeds up again. It is just not good, do you see any problems?

Try to normalize your moveDirection vector. Also, why are you applying gravity to it?