Cannot read 'setLocalEulerAngles' of undefined

Help! My code below is not working.

          var zrot = pc.math.clamp(this.velocity, -2, -0.75);
          zrot += 1;
          this.entity.setLocalEulerAngles(0, 0, zrot * 90);
            
            setTimeout(function () {
          var zrot3 = pc.math.clamp(this.velocity, -2, -0.75);
          zrot3 -= 1;
          this.entity.setLocalEulerAngles(0, 0, zrot * -180);
            }, 600);

I do not understand why as I created another one specifically for to be used inside a scope.
Please help? :slight_smile:

You need to change the context of your setTimeout callback function, so this refers to the script and not to that function.

Try this:

setTimeout(function () {
          var zrot3 = pc.math.clamp(this.velocity, -2, -0.75);
          zrot3 -= 1;
          this.entity.setLocalEulerAngles(0, 0, zrot * -180);
            }.bind(this) , 600);

Thanks @Leonidas
This fixed my errors but unfortunately this did not work. After the 600ms it did nothing. :confused:

You will have to debug your zrot3 variable inside your timeout.

By the way, in the timeout you are setting the zrot3 variable, but you doesn’t use it, in the method you use again zrot.

@Leonidas -
I have added and modified some code:

             var playings = (this.state === 'play');
            
            if (playings) {
                
                        if (app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT)) {
            setTimeout(function () {
         var zrot = pc.math.clamp(this.velocity, -2, -0.75);
          zrot += 1;
          this.entity.setLocalEulerAngles(0, 0, zrot + 22.5);
         }.bind(this) , 0.01);
                            
                setTimeout(function () {
          var zrot3 = pc.math.clamp(this.velocity, -2, -0.75);
          zrot3 += 1;
          this.entity.setLocalEulerAngles(0, 0, zrot3 * 90);
                }.bind(this) , 750);
        }
    }

This now works, but not smoothly (as I expected with zrot3 * 90)
after the 750ms, it faces instantly down. Any suggestions? :slight_smile:

setTimeout will execute the code once, after 750 ms, but it won’t do any animating. It will set the rotation directly to the final value.

You have to do the animation per frame, in your update method, using dt to smooth each rotation. Here is an example on how to animate an entity in the update method:

I have tried this now, and using dt freezes the bird,
I tried doing zrot3 * dt but this froze too

Could you create a sample repro project that tries to do only that?

It will make it easier to debug and also it will help you learn how to animate things.

Hi @Leonidas
I have been trying for at least 1 hour now with no success. I have looked on Playcanvas forums on many, many threads, and none of these seem to work for me