Hello everyone,
I am trying to move player ship based on mouse event i.e. mousemove event. But when mouse cursor intersects player ship there is unexpected behavior like flickering. the code which I used for the movement is attached below please do have a look.
PlayerController.prototype.update = function (dt) {
if (this.pointerLocked) {
this.camera.worldToScreen(this.entity.position, this.screenPos);
this.screenPos.z = 0;
this.directionToMouse.sub2(this.pointerPos, this.screenPos);
this.directionToMouse.y *= -1;
var angle = Math.atan2(this.directionToMouse.x, this.directionToMouse.y) * pc.math.RAD_TO_DEG;
var targetRotation = this.targetRotation.clone().setFromEulerAngles(90, 0, 90 - angle);
var newRotation = this.newRotation.slerp(this.entity.getRotation(), targetRotation, 0.2);// slerp for smooth rotation
this.directionToMouse.normalize().scale(this.movementSpeed);
let condition = this.isPlayerInsideTheBoundary(this.entity.getPosition(), this.directionToMouse);
// condition === true ? console.log("inside") : console.log("outside");
if (condition) {
let currentPlayerPosition = this.entity.getPosition()
let targetPosition = currentPlayerPosition.clone().add(this.directionToMouse.clone());
this.entity.setRotation(newRotation);
this.entity.rigidbody.teleport(targetPosition);
let cameraCondition = this.isPlayerCloseToBoundary(this.entity.getPosition())
if (cameraCondition.isClose) {
console.log(cameraCondition.side)
this.adjustCameraPosition(cameraCondition.side, this.directionToMouse)
} else {
let cameraPosition = new pc.Vec3();
cameraPosition.lerp(this.cameraEntity.getPosition().clone(), this.entity.getPosition().clone(), this.cameraLerpFactor);
this.cameraEntity.setPosition(cameraPosition.x, cameraPosition.y, this.cameraEntity.getPosition().clone().z);
}
}
}
}
NOTE : the function isPlayerInsideTheBoundary is just to check whether the player is inside the specified boundary or Not.