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
- putting the camera update position in lateUpdate()
- 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