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?
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
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.
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.
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
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:
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
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
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.