I know there’s already an FPS script that makes the player’s movements relative to the camera’s rotation, but I want to know how it accomplishes that, and how I should integrate it into my script. Here’s my player code:
var PlayerController = pc.createScript('playerController');
//Variables
var acc = 20;
var frc = acc;
var dec = 4;
var limit = 400;
var xsp = 0;
var zsp = 0;
var goingRight;
var goingFront;
// initialize code called once per entity
PlayerController.prototype.initialize = function() {
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
this.app.keyboard.on(pc.EVENT_KEYUP, this.onKeyUp, this);
};
// update code called every frame
PlayerController.prototype.update = function(dt) {
//Functions
this.MainBase(dt);
};
PlayerController.prototype.MainBase = function(dt) {
//Main
this.entity.rigidbody.linearVelocity = new pc.Vec3(xsp * dt, this.entity.rigidbody.linearVelocity.y, zsp * dt);
//Basic Movement
if (this.app.keyboard.isPressed(pc.KEY_D)) {
xsp += acc;
goingRight = true;
}
if (this.app.keyboard.isPressed(pc.KEY_A)) {
xsp -= acc;
goingRight = false;
}
if (this.app.keyboard.isPressed(pc.KEY_W)) {
zsp -= acc;
goingFront = true;
}
if (this.app.keyboard.isPressed(pc.KEY_S)) {
zsp += acc;
goingFront = false;
}
if (!this.app.keyboard.isPressed(pc.KEY_D) && !this.app.keyboard.isPressed(pc.KEY_A)) {
goingRight = null;
}
if (!this.app.keyboard.isPressed(pc.KEY_W) && !this.app.keyboard.isPressed(pc.KEY_S)) {
goingFront = null;
}
//Deceleration / Friciton
if (goingRight === true && xsp < 0) {
xsp += dec;
}
if (goingRight === false && xsp > 0) {
xsp -= dec;
}
if (goingFront === true && zsp > 0) {
zsp -= dec;
}
if (goingFront === false && zsp < 0) {
zsp += dec;
}
if ((xsp > 0 || xsp < 0) && goingRight === null) {
if (xsp > 0) {
xsp -= frc;
}
if (xsp < 0) {
xsp += frc;
}
if (xsp < frc && xsp > -frc) {
xsp = 0;
}
}
if ((zsp > 0 || zsp < 0) && goingFront === null) {
if (zsp > 0) {
zsp -= frc;
}
if (zsp < 0) {
zsp += frc;
}
if (zsp < frc && zsp > -frc) {
zsp = 0;
}
}
//Limit
if (xsp > limit || xsp < -limit) {
if (xsp > limit) {
xsp = limit;
}
if (xsp < -limit) {
xsp = -limit;
}
}
if (zsp > limit || zsp < -limit) {
if (zsp > limit) {
zsp = limit;
}
if (zsp < -limit) {
zsp = -limit;
}
}
//Rotations
if (goingRight === true && goingFront === true) {
this.entity.rigidbody.teleport(this.entity.getPosition().x, this.entity.getPosition().y, this.entity.getPosition().z, this.entity.getEulerAngles().x, -45, this.entity.getEulerAngles().z);
}
if (goingRight === false && goingFront === true) {
this.entity.rigidbody.teleport(this.entity.getPosition().x, this.entity.getPosition().y, this.entity.getPosition().z, this.entity.getEulerAngles().x, 45, this.entity.getEulerAngles().z);
}
if (goingRight === true && goingFront === false) {
this.entity.rigidbody.teleport(this.entity.getPosition().x, this.entity.getPosition().y, this.entity.getPosition().z, this.entity.getEulerAngles().x, 45, this.entity.getEulerAngles().z);
}
if (goingRight === false && goingFront === false) {
this.entity.rigidbody.teleport(this.entity.getPosition().x, this.entity.getPosition().y, this.entity.getPosition().z, this.entity.getEulerAngles().x, -45, this.entity.getEulerAngles().z);
}
if (goingRight === true && goingFront === null) {
this.entity.rigidbody.teleport(this.entity.getPosition().x, this.entity.getPosition().y, this.entity.getPosition().z, this.entity.getEulerAngles().x, 90, this.entity.getEulerAngles().z);
}
if (goingRight === false && goingFront === null) {
this.entity.rigidbody.teleport(this.entity.getPosition().x, this.entity.getPosition().y, this.entity.getPosition().z, this.entity.getEulerAngles().x, 90, this.entity.getEulerAngles().z);
}
if (goingRight === null && goingFront === true) {
this.entity.rigidbody.teleport(this.entity.getPosition().x, this.entity.getPosition().y, this.entity.getPosition().z, this.entity.getEulerAngles().x, 0, this.entity.getEulerAngles().z);
}
if (goingRight === null && goingFront === false) {
this.entity.rigidbody.teleport(this.entity.getPosition().x, this.entity.getPosition().y, this.entity.getPosition().z, this.entity.getEulerAngles().x, 0, this.entity.getEulerAngles().z);
}
if (goingRight === null && goingFront === null) {
this.entity.rigidbody.teleport(this.entity.getPosition().x, this.entity.getPosition().y, this.entity.getPosition().z, this.entity.getEulerAngles().x, this.entity.getEulerAngles().y, this.entity.getEulerAngles().z);
}
};
// swap method called for script hot-reloading
// inherit your script state here
// PlayerController.prototype.swap = function(old) { };
// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/
So how would I go about making the movements relative to the rotation of the camera?
Link To Editor: https://playcanvas.com/editor/scene/953748