Rotation of Wheel

Hello all,

In our new game, I am trying to rotate a wheel when the key D is pressed. Code is below.

var WheelRotation = pc.createScript('wheelRotation');

// initialize code called once per entity
WheelRotation.prototype.initialize = function() {
    
};

// update code called every frame
WheelRotation.prototype.update = function(dt) {
    
    if (this.app.keyboard.isPressed(pc.KEY_D)) {
        this.entity.rotateLocal(10, 0, 0);
    }
};

// swap method called for script hot-reloading
// inherit your script state here
// WheelRotation.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

As evident, the code is extremely simple. However, the wheel does not rotate perfectly - https://launch.playcanvas.com/838901?debug=true - the tyre and rim are rotating at different speeds.

40%20AM

I am trying to keep the Wheel Parent in the center of the ‘treckter’ model, but I am not getting the perfect center position. The rim and the tyre are separate meshes. Is there any way I could solve this issue?

Hi @DevilZ,

I think the issue is with the pivot point that is set for both objects in Blender. That has to be the same for both objects and it has to be at the center, where the axis that both objects are supposed to rotate around in Playcanvas.

Try playing with the pivot point in Blender, re-export and check how the rotation is affected.

Is the wheel and the rim two separate entities in PlayCanvas?

No. I’m using the entire model @yaustar

Unfortunately @leonidas, I’m not great at blender. Could you elaborate a bit further? How do I change the pivot point?

There are plenty of tutorials online, here is a simple one for an older version of Blender, but I think it is still valid:

You should make sure that your objects’ center and the pivot point match, before you export.

Thanks. I’ll check it out.

The tyre is not rotating at a different speed to the rim, it just looks like it because of the pattern. If you slow the speed right down, you can see that the rim and the tyre rotate at the same rate

See: https://playcanvas.com/editor/scene/838942

You do also need to correct the pivot point of the middle so it’s in the middle as it makes so much easier to position it in the app.

1 Like

I’ll take a look. Thanks.

EDIT - Works Perfectly.