Rotation.lerp is not a function in FPS

Sorry for the late reply. I couldn’t look into the recoil thing since i had other stuff to deal with first. I did as you asked. I added a function inside my firstPersonMovement script then created an event listener for it to fire inside the gun script when the weapon is fired. This is how it looks like in the firstPersonMovement script:

FirstPersonMovement.prototype.initialize = function () {
    this.force = new pc.Vec3();
    this.eulers = new pc.Vec3();

    var app = this.app;
this.app.on('gun:recoil', this.recoil, this);

FirstPersonMovement.prototype.recoil = function () {
    
    this.eulers.y += pc.math.random(1, 5);
};

this is the fire event inside the gun script:

Gun.prototype.recoil = function () {
    this.app.fire('gun:recoil');
};

this function is called inside the the shoot function.

With this script i managed to rotate the weapons in the y axis when i shoot. I think this is the most simple and barebones recoil script one can ever write. How do you recommend me to proceed from this point?

Think about the features you want here and work on it one at a time.

eg:

Do you want recoil recovery?

Do you want recoil to be bigger the longer you hold down fire?

etc

1 Like

what is the logic behind recoil recovery? Do i need to reset the Vec3 or make the camera drop down a few units right after firing the gun? It needs to be a springy movement, when the gun reaches it’s highest point of recoil, it will automatically go back to it’s original position, or is it? I couldn’t quite figure it out

Depends on what you want logic wise.

I’ve based mine on that when the user stops firing, it would return to the starting position of when you started firing.

See How Does Recoil Work in CSGO? - YouTube

The problems I ran into and solved in the project I’ve linked before, is what happens if the player tries to compensate for the recoil. Where should the aim end up when the user stops firing etc.

And all of this is dependent on the feature you want for this.

1 Like

Do you want recoil recovery?: check

Do you want recoil to be bigger the longer you hold down fire?: also check

you can check the project if you want here:
https://playcanvas.com/editor/scene/1528185

I haven’t implemented the fire rating mechanic yet, so i only applied recoil to the pistol for now. My problem is that the recoil animation looks very choppy. Can you give me any tips to make it smoother?

My advice is to get the logic of how you want it to work on paper first. Then go through one behaviour at a time.

Why it looks choppy is probably because of a combination of each recoil effect being quite high and after you release the mouse button, it jumps back to another rotation vale instead of moving towards it.

Here’s a link to my project again if you want to see how I handled all of this: https://playcanvas.com/project/950571/overview/fps-pit

1 Like

Ok, this line is what creates the recoil action right?

this.eyesEntity.setLocalEulerAngles(this._anglePitch + this._recoilPitch, this._angleYaw, 0);

Which one is the code piece that handles recoil recovery? and you fire events from a script called “weapon” where is that located, i can’t find it anywhere

Not directly no. It sets the angle on the camera based using the values from the mouse controls and the recoil logic.

Look at where the variables are modified in the project. Where is _anglePitch modified? Where is _recoilPitch modified?

Use the find in files function in the code editor

1 Like

is it weapon-projectile?

I really don’t understand what’s going on in your script eventhough i genuinely tried hard a few times to figure that out. But in another topic you gave me this reply:

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

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

So i assume that this is the line which makes recoil work. If i try to break down this line; Utils is a reference to the utils helper script. You get the moveTowards variable from utils. MoveTowards is actually a Unity/C# method which is alternative to lerp. That’s why you said “this is my lerp.” Then you set the Vec3 parameters of moveTowards with x=recoilPitch, y=0 and z= recoilRecoveryRate and multiply them with delta time in order for the action to happen at a speed independent from game’s frame rate. So is this what made your recoil animation smooth, moveTowards ?

I think another factor my animation looks choppy is that my recoil action is happening at the game’s frame rate. I need to make it happen in delta time

moveTowards is a function that takes one value, a target value and a ‘speed’ value to move the first value towards the second.

With that in mind, I’m basically setting _recoilPitch back to 0. What effect in the game do you think it has? What would you think you be seeing?

so it has nothing to do with this?

I’m basically setting _recoilPitch back to 0.

So that’s recoil recovery. But you make the crosshair return to the first firing position after you stop firing, at a constant speed. moveTowards make an object move from one position to another at a constant speed. It’s doesn’t do what lerp does exactly but it’s somewhat an alternative to lerp in a way in terms of making the animations look better. At least they don’t look choppy this way

It’s the same functionality, yes.

Correct. Almost. It’s not when the user stops firing, it’s trying to recover all the time.

It doesn’t have to be constant speed and I could apply acceleration in the calculation. Or you could use lerp too. It’s just different behaviour logic.

1 Like