Implementing Recoil

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.

I understand , thank you

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?

In a nutshell, yes.

In my case, I had two pitch properties in my camera, one from player controls and the other for recoil so that I could handle recovery

(see: https://playcanvas.com/editor/code/950571?tabs=90738144)

This is not final and was planning to change it slightly as I was not happy with the recovery logic.

I’ve also separated the logic so that the weapon passes the data of recoil effect to the camera when it fires

1 Like

what’s alpha variable’s function in this script?

What function? My ‘lerp’ is done via this line

        this._recoilPitch = Utils.moveTowards(this._recoilPitch, 0, this._recoilRecoveryRate * dt);
1 Like

I can adjust the recoil force from within the my gun script but how can i rotate the camera witihin this script? gun script is attached to guns only

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.

This is my project: https://playcanvas.com/project/950571/overview/fps-pit

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?

You can do it a number of ways. I’m just telling you how I did it. If you want to do everything in the gun script, you can

1 Like

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:

Can you tell me why i’m getting those errors?

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.

1 Like

setEvents is not written as it needs to be and you need to follow how other functions are declared in that script.

Yes, i know. I didn’t want to write the whole thing before i show you the errors i was getting

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

You have to look at what my whole project is doing a whole.

What is my project using ‘eye anchor’ for? Does it match what your camera handler is doing?

I’m bogged down, this script is too complex for me. I need to take a simpler route. @yaustar thx for the help anyway