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?


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