So I want to make the camera recoil when left click is pressed , everything is right except the problem that if i use this.entity.setLocalEulerAngles to set the Euler angles but it gets overwritten by the first person camera which calls itself per frame , I don’t know how to go about fixing this , any Help is appreciated
Here’s the code
Recoil.prototype.initialize = function() {
this.lerpedRotation = new pc.Vec3 ();
this.Lerpspeed = 1;
this.alpha = 0;
};
// update code called every frame
Recoil.prototype.update = function(dt) {
this.alpha += this.Lerpspeed * dt;
this.firstloc = this.entity.getLocalEulerAngles();
this.targetRotation = new pc.Vec3 ( this.firstloc.x+ 10,this.firstloc.y , this.firstloc.z);
if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT)) {// this.lerpedRotation.lerp (this.entity.getEulerAngles(), this.targetRotation, this.alpha);
this.lerpedRotation.lerp (this.entity.getLocalEulerAngles(), this.targetRotation, this.alpha); // this.finalRot.lerp(this.lerpedRotation, this.firstloc1, this.speed * dt) ;
// Update the entity's Rotation to the lerped position
this.entity.setLocalEulerAngles (this.lerpedRotation);
}
I would move the code to the first person camera. Assuming you want different recoil patterns with different weapons, you can pass a function to the camera based on the weapon that stores the recoil pattern/behaviour.
So this function needs to be inside the gun script right? The script where weapon controls are handled. So i can change the recoiling values of each weapon from the editor. I’ll do the recoil by changing the x rotation of the camera from within that script then?
You have to pass or retrieve the recoil force from the gun script.
In my case, my camera script is listening for an event on the weapon entity. The weapon entity fires an event every time it fires a bullet and passes data like the recoil effect in the event.
The camera script callback is fired and applies the recoil.
ok, then do i need to create a camera script and fire the recoil calback from it. Is that the only way? Can’t i do the whole thing inside the gun script?
agent718’s script works but there are some issues and it doesn’t work very great on my project like recoil recovery and camera clamp etc. I’m trying your script now but i’m getting errors from the get go. Your script has an entirely different structure then i’m familiar with. This is how my script looks like right now:
My scripts were in ES6 JS (personal project so was experimenting)
PlayCanvas documentation and tutorials are using ES5.
So you need to declare functions in the same way as the default scripts layout. At the moment in your script setEvents is not written as it needs to be and you need to follow how other functions are declared in that script.
Does “Eyes Anchor” entity serve as a “camera holder”? I don’t have “eyes anchor”, can i use “camera holder” for the same purpose?My camera entity is attached to that