How to do tilt control?

Hi, how to do tilt control for player movement (player can move horizontal left to right movement ),
can anyone help me .

If you mean mobile phone tilting … you can easily achieve that using plain JavaScript:

This is an event that fires when your phone tilts and provides you with the current rotation.

You can subscribe to this event as well and get the acceleration, how fast your device rotated. Just make sure you check the browser compatibility table to make sure where it is supported.

1 Like

Hey @Veereenddra, @Leonidas is spot on. Any browser API is available via JavaScript. I once wrote a script that emitted events for x and z rotation to roll a ball around a maze on screen. It looked a bit like this:

var Tilt = pc.createScript('tilt');

Tilt.attributes.add('maxAngle', {
    type: 'number',
    title: 'Max Angle',
    description: 'The maximum allowable tilt angle',
    default: 90,
    min: 0,
    max: 90,
});

Tilt.prototype.initialize = function() {
    this.handleOrientation = this.handleOrientation.bind(this);
    window.addEventListener('deviceorientation', this.handleOrientation);
};

Tilt.prototype.handleOrientation = function (e) {
    var x = pc.math.clamp(-e.beta, -this.maxAngle, this.maxAngle);
    var z = pc.math.clamp(e.gamma, -this.maxAngle, this.maxAngle);
    this.app.fire('input:tilt', x, z);
};

Then any other script could subscribe to those events with a simple this.app.on('input:tilt', handleTilt).

1 Like