Camera rotation

When the shift key is pressed I want the cam to rotate back an forth.
HOWEVER, if the shift key is released ONLY the y rotation are set back to zero.

Can someone please write a script for that please?

Hi @Mason_Rocchio! I’m not sure what you mean with back and forth. Below an example how to rotate the camera to the left when the SHIFT key is pressed.

var Rotate = pc.createScript('rotate');

// initialize code called once per entity
Rotate.prototype.initialize = function() {
    // store rotation of camera before rotating
    this.angles = this.entity.getEulerAngles();
};

// update code called every frame
Rotate.prototype.update = function(dt) {
    // check if SHIFT key is pressed
    if (this.app.keyboard.isPressed(pc.KEY_SHIFT)) {
        // rotate camera on Y axis
        this.entity.rotateLocal(0,10*dt,0);
    } else {
        // set camera back to original rotation
        this.entity.setLocalEulerAngles(this.angles);
    }
};

You can also use a tween to do this. A tween should be a better option anyway, if you want to rotate the camera for example from left to right and back.

https://developer.playcanvas.com/en/tutorials/tweening/

I mean it rotates to the left then the right over and over until the shift key is released

How do you want to use it? It’s a little more complex if you want to rotate it to the left first and then the same amount to the right. Like looking to left and right when you want to cross over the street. In that case you first need to rotate to the left 90 degrees then to the right 180 degrees.

yes, but when the shift key is released the y rotation is back to zero

in other words
if shift key pressed
90 degrees to the right
90 degrees to the left
repeat until shift key was released

Below an example of using a tween. Make sure the tween script is in your project if you copied the code. Please let me know when you are finished, then I will remove the project.

https://playcanvas.com/editor/scene/1314379

1 Like