Rotation.lerp is not a function in FPS

i’m getting the same error for this line :

And this is the error msg:

Hi @Ronin,

Check this post: [SOLVED] Rotation.lerp is not a function - #2 by Leonidas

In your case, line 41 is causing the issue.

JS doesn’t have overload operators so you can’t use += on a pc.Vec3. You have to use the function .add.

I print targetRotation on console :

this.targetRotation += new pc.Quat(this.recoilX, pc.math.random(-this.recoilY, this.recoilY), pc.math.random(-this.recoilZ, this.recoilZ));

this is what i get :

The second time i fire, i get NAN for the recoilX paramaters and the gun(actually camera) is not moving. Any idea why?

It’s the same problem I mentioned before:

Also, if this.targetRotation is still a pc.Vec3, you shouldn’t be trying to do an operation with a pc.Quat as they are different types and will lead to undefined behaviour.

JavaScript Set add() Method - GeeksforGeeks is this what you are referring to?

I defined targetRotation as a pc.Quat in the init function.

This is how my script looks:

I call recoilFire function from the shoot function. I think one of my mistakes is that i use the getRotation, setRotation functions wrong somehow. Camera rotates once when i shoot(but not as i desired)the second time i shoot the camera completely goes crazy

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);

var kickBack = new pc.Vec3(0, 0, kickbackZ);


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


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 () {

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:

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