How to rotate an object continuously in your game environment?

I’m making a 3d game right now, and in my environment, I want an object to rotate continuously without the player having to interact with it. How should I do that? What code should I use? or is there another way to go about that?

Thanks!

Hi @zero2,

A simple way to do it:

  1. Create a new script
  2. Attach it to your entity on a script component
  3. Put the following code in the script .update() method:
const rotationSpeed = 3.0;
this.entity.rotateLocal(0, rotationSpeed * dt, 0);
1 Like

This will allow you to control the rotation speed from the PlayCanvas Editor, making it easier to tweak the speed without having to modify the script each time. Here’s how you would do it:

var Rotation = pc.createScript('rotation');

// Add speed attribute
Rotation.attributes.add('speed', { type: 'number', default: 30 });

// Initialize code called once per entity
Rotation.prototype.initialize = function() {
    // The rotation speed is now controlled by the speed attribute
    // You can adjust this value in the PlayCanvas Editor
};

// Update code called every frame
Rotation.prototype.update = function(dt) {
    // Rotate the entity around the Y-axis
    this.entity.rotate(0, this.speed * dt, 0);
};

Hi, I tried to use a custom rotation axis here. This is my code:

this.entity.rotateLocal(this.localRotationAxis.mulScalar(dt * this.rotationSpeed));

However, this leads to the entity either not rotating or rotating very fast, depending on whether rotationSpeed is greater than 60 or not.
I assume this is because of lacking precision of mulScalar().

How can I achieve a continuous local rotation on any axis?