So i have a camera that follows a player around when the player moves i have figured out how to rotate the light with the camera as the player rotates But if i walk forward or backwards the light doesn’t follow the camera and thus the light doesn’t follow the player here is my full code the flash light is the problem part of the code, it’s like the x,y,z isn’t updating…
var flashLight;
var flashLight2;
var spotLight2;
var spotLight3;
var spotLight;
export const third_person_camera = (() => {
class ThirdPersonCamera extends entity.Component {
constructor(params) {
super();
this._params = params;
this._camera = params.camera;
this._currentPosition = new THREE.Vector3();
this._currentLookat = new THREE.Vector3();
this._flashLightr();
}
_flashLightr() {
flashLight = new THREE.Mesh(new THREE.CylinderGeometry(1, 1, 7, 20), new THREE.MeshPhongMaterial({color:0x000000}));
flashLight.rotateX(Math.PI/2);
spotLight = new THREE.SpotLight(0xffffff, 0.5, 150);
spotLight.power = 4000;
spotLight.angle = 0.5;
spotLight.decay = 2;
spotLight.penumbra = 0.1;
spotLight.distance = 200;
spotLight.castShadow = true;
spotLight.rotateX(Math.PI/2);
spotLight2 = new THREE.SpotLight(0xCCCCCC, 0.5, 150);
spotLight2.power = 2000;
spotLight2.angle = 0.55;
spotLight2.decay = 2;
spotLight2.penumbra = 0.1;
spotLight2.distance = 200;
spotLight2.castShadow = true;
spotLight2.rotateX(Math.PI/2);
spotLight3 = new THREE.SpotLight(0xFFFFFF, 0.5, 150);
spotLight3.power = 4000;
spotLight3.angle = 0.6;
spotLight3.decay = 2.5;
spotLight3.penumbra = 0.1;
spotLight3.distance = 200;
spotLight3.castShadow = true;
spotLight3.rotateX(Math.PI/2);
flashLight.add(spotLight);
flashLight.add(spotLight.target);
flashLight.add(spotLight2);
flashLight.add(spotLight2.target);
flashLight.add(spotLight3);
flashLight.add(spotLight3.target);
// flashLight.position.copy(camera.position);
// add
this._camera.add(flashLight);
}
_CalculateIdealOffset() {
const idealOffset = new THREE.Vector3(-1.6, 7, -9);
idealOffset.applyQuaternion(this._params.target._rotation);
idealOffset.add(this._params.target._position);
return idealOffset;
}
_CalculateIdealLookat() {
const idealLookat = new THREE.Vector3(0, 5, 20);
idealLookat.applyQuaternion(this._params.target._rotation);
idealLookat.add(this._params.target._position);
return idealLookat;
}
Update(timeElapsed) {
const idealOffset = this._CalculateIdealOffset();
const idealLookat = this._CalculateIdealLookat();
// const t = 0.05;
// const t = 4.0 * timeElapsed;
const t = 1.0 - Math.pow(0.01, timeElapsed);
this._currentPosition.lerp(idealOffset, t);
this._currentLookat.lerp(idealLookat, t);
this._camera.position.copy(this._currentPosition);
this._camera.lookAt(this._currentLookat);
flashLight.position.copy(this._currentPosition);
flashLight.position.x += 1;
flashLight.position.y -= 1;
flashLight.position.z += 1;
// flashLight.lookAt(this._currentLookat);
}
}
return {
ThirdPersonCamera: ThirdPersonCamera
};
})();