Weapon Sway and Bobbing script causes weapon to disappear

I created a weapon sway/bobbing script, but it causes the weapon to disappear when I use it. Not sure what the problem is.
Here is my project: PlayCanvas | HTML5 Game Engine

Here is the code for the specific script:

WeaponSway.prototype.initialize = function() {
    this.intensity = 0.005; // adjust this to increase/decrease sway intensity
    this.speed = 500; // adjust this to increase/decrease sway speed
    this.defaultPosition = this.entity.getLocalPosition().clone();
    this.sideIntensity = 0.003; // adjust this to increase/decrease side-to-side sway intensity

WeaponSway.prototype.update = function(dt) {
    var camera = this.entity.parent;
    var currentPosition = camera.getLocalPosition();    
    if (!currentPosition) {
    var xDiff = (this.defaultPosition.x - currentPosition.x) * this.intensity;
    var yDiff = (this.defaultPosition.y - currentPosition.y) * this.intensity;
    var zDiff = (this.defaultPosition.z - currentPosition.z) * this.sideIntensity;
    this.entity.setLocalPosition(this.defaultPosition.x - xDiff, this.defaultPosition.y - yDiff, this.defaultPosition.z + zDiff);
    this.defaultPosition.lerp(currentPosition, this.speed * dt);
    console.log("Weapon position: ", this.entity.getLocalPosition());

lerp requires two vec3s to be passed into it and the lerp value. It’s currently disappearing because it’s calculating NaNs due to wrong objects and not enough parameters being passed to it.


    this.defaultPosition.lerp(this.defaultPosition, currentPosition, this.speed * dt);

That worked for about a half second, then it disappeared again.

You will need to debug what values you are getting for the position if the sway and see it they match expectations

Looking at your currently output:

It looks like the math is off in your code for working out where to lerp to as the numbers get very big very quickly

OK, so I tried adjusting this.speed, which seems to have brought it back, but now everytime I launch the model slowly travels to the location of the camera (I believe the speed at which it does so is relative to this.speed).

If the weapon is a child of camera, the weapon local position and rotation is relative to the camera.

So trying to lerp the weapon local position to the camera’s local position is going to give odd results.

You need to re-think on how you implement the sway of the weapon as this approach isn’t correct.

Alright, I will try sinusoidal motion instead, and see if it works.

Alright, I tried using Math.sin() instead, but now the weapon is in an odd position and not swaying.
Edit: I managed to fix the “odd position” of the weapon, but the weapon still does not move.
Edit 2: Script is now fixed

mind if I borrow that script?