How to ensure strict camera following the player without camera jerkiness or delayed following?

Hi,
The camera follows late and with jerkiness the player

in update() i’m simply setting the velocity value and getting it’s updated position to set to camera node

import { _decorator, Camera, Component, EventKeyboard, input, Input, KeyCode, math, Node, RigidBody, RigidBody2D, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('cubeMovement')
export class cubeMovement extends Component {


    public rigidBody:RigidBody2D;

    left
    right
    speed
   


    @property(Camera)
    cam:Camera

    
    onLoad() {
        this.rigidBody = this.getComponent(RigidBody2D);
        this.left = false;
        this.right = false;
        this.speed = 20;

        input.on(Input.EventType.KEY_DOWN, (event: EventKeyboard)=>{

            if(KeyCode.ARROW_LEFT == event.keyCode){
                this.left = true;
            }
            else if(KeyCode.ARROW_RIGHT == event.keyCode){
                this.right = true;
            }

        }, this);


        input.on(Input.EventType.KEY_UP, (event: EventKeyboard)=>{
            this.left = false;
            this.right = false;
        }, this);



    }

    protected lateUpdate(dt: number): void {
        
    }

    update(deltaTime: number) {


        if(this.left){
            this.rigidBody.linearVelocity = new Vec2(-this.speed, this.rigidBody.linearVelocity.y);
        }
        else if(this.right){
            this.rigidBody.linearVelocity = new Vec2(this.speed, this.rigidBody.linearVelocity.y);
        }
        else{
            this.rigidBody.linearVelocity = new Vec2(0, 0);
        }

        this.cam.node.setPosition(this.node.position.x, this.node.position.y);
                
    }
}


I have tried following solutions and none of them works

  1. putting the camera update position in lateUpdate()
  2. tried lerp where if I set value 0 (because I need instant camera following) it start showing like original jerkness camera issue

Any idea how to fix?

Thanks

You can use Vec3.lerp to create a linear interpolation of target position, then set it to the camera node position.

I tried it and it works like smooth following but I wanted to have exact camera following without animation, setting the value of time of lerp to 0 start showing the same jerkiness effect, for the time being putting the camera as a child inside the player node serve the purpose but want to do the same being without the child of player node & without lerp

Could you provide the demo for me to check?

Try moving the following code line into the lateUpdate method.

this.cam.node.setPosition(this.node.position.x, this.node.position.y);