Rotation.lerp is not a function in FPS

No, I’m saying that unlike C# and other languages, JS doesn’t support custom logic for operators like +=, + etc

the link i shared, isn’t it about the .add function that you mentioned

No, that’s a specific method for the Set class in JS

This part is correct right? Pls at least tell me which part i need to fix

Gun.prototype.initialize = function () {
    this.targetRotation = new pc.Quat();
    this.currentRotation = new pc.Quat();
    this.speed = 1;
};

In slerp, you need a quaternion to interpolate from and interpolate to. These two variables are created for that purpose. Then i added speed variable to determine how fast the entity rotates. Is everything correct up to this point? Pls at least let me know if i’m going at the right direction or not

It’s not wrong but also there’s not really much code here to comment on yet. Personally, I wouldn’t use quaternions for this as I find it harder to control individual axes of motion (as you saw in my project)

1 Like

Personally, I wouldn’t use quaternions for this as I find it harder to control individual axes of motion (as you saw in my project)

What about vector 3 lerp? I was told that the quaternions are the best way rotate an object with slerp so i’m giving a shot with this one. In fact in this topic: Implementing Recoil

the OP’s script worked on me to rotate the camera but A: the animation wasn’t smooth as it was supposed to be or i felt it so. I’m not %100 with that one
B: Camera goes into a loop and gets stuck on a certain angle after you fire a few shots. I couldn’t find a way to add recoil recovery. In your script you have recoil recovery but you said you aren’t totally satisfied the way it works
C: I couldn’t find a way to adjust the recoil force in that script. No matter what value i enter, the camera rotates the same amount after the first shot

All I can give is my experience and problems I faced.

Turns out I already had fixed it but forgot. It’s in a good place now

1 Like

by .add do you mean like this? this.targetRotation.x, this.targetRotation.y

A good place to look is usually the API docs as it can give you a good idea of what you can do for each class/object.

In this case, I mentioned to use the add function on the pc.Vec3, if you look at the API docs: Vec3 | PlayCanvas API Reference

You can see the add function as well as many other functions that can be useful to you in the future.

1 Like

how can i do it with this line ? “targetRotation -= new Vector3(0, 0, kickbackZ);” there is only one vector 3 here. How can i do this here:

var a = new pc.Vec3(10, 10, 10);
var b = new pc.Vec3(20, 20, 20);

a.sub(b);
var kickBack = new pc.Vec3(0, 0, kickbackZ);
targetRotation.sub(kickBack);

Or

targetRotation.sub(new pc.Vec3(0, 0, kickbackZ);

Or

targetRotation.z -= kickbackZ;
1 Like

This is my new script. I got this from another Unity video tutorial.

var Recoil = pc.createScript('recoil');
Recoil .attributes.add('cameraEntity', { type: 'entity' });
Recoil .attributes.add('minX', { type: 'number' });
Recoil .attributes.add('MaxX', { type: 'number' });
Recoil .attributes.add('minY', { type: 'number' });
Recoil .attributes.add('maxY', { type: 'number' });

Recoil.prototype.initialize = function () {
    this.rot = new pc.Vec3();
};

Recoil.prototype.update = function (dt) {
    this.rot = this.cameraEntity.getLocalEulerAngles();

    if (this.rot.x != 0 || this.rot.y != 0) {
        this.cameraEntity.setRotation(new pc.Quat(this.cameraEntity.getRotation(), new pc.Quat(0, 0, 0)).slerp, dt * 3);
    }
};

Recoil.prototype.shoot = function () {
this.recoil();
};

Recoil.prototype.recoil= function () {
var recX = pc.math.random(this.minX, this.maxX);
var recY = pc.math.random(this.minY, this.maxY);
this.cameraEntity.setRotation(new pc.Quat(this.rot.x - recY, this.rot.y + recX, this.rot.z));
};

When i shoot the screen goes like this:

can you tell me what exactly is wrong with this script or how can i debug it?

these are the values for the minX, maxX, minY, maxY variables:
Untitled

At a glance, quaternions take 4 parameters (xyzw) and aren’t in euler angles so this would give you bad values in the quaternion with w being undefined.

Not sure what you doing here as setRotation either takes one parameter (another quaternion) or 4 parameters (xyzw) Entity | PlayCanvas API Reference

1 Like

I would say at this stage, take it a step at the time. Don’t worry about recovery and just try getting the gun to recoil up.

1 Like

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