Hi again all,
Back again with some more coding questions.
I have a quick and dirty movement system set up for my player object. (the object is a craft that the player flies around, with a camera attached to the back). It’s all very simple at the moment and needs work.
The controls (w,a,s,d, space and c) work fine to move it forward, back, left, right, up and down, And I have the E key set as a very simple ‘afterburner’ key, with ‘steering’ handled by the mouse, and it’s all working as it should.
Now the problem I’m having is that the object just starts and stops dead, which is obviously a bit nasty, I did have another method setup (from the lunar lander tutorial) which had nice intertia etc. on it, but I couldn’t seem to get it to work in terms of flying in the direction the mouse was pointed.
Also, another problem (which I understand from reading on here is because I’m not using the ‘entity.rigidbody’ method to move my ship, so I guess that would be fixed with the movement system using a force.
Here is my code if it’s needed
pc.script.create('player', function (app) {
// Creates a new Player instance
var Player = function (entity) {
this.entity = entity;
this.thrustVec = new pc.Vec3();
// Camera euler angle rotation around x and y axes
var eulers = this.entity.getEulerAngles()
this.ex = eulers.x;
this.ey = eulers.y;
// Disabling the app menu stops the browser displaying a menu when
// you right-click the page
app.mouse.disableContextMenu();
app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
};
Player.prototype = {
update: function (dt) {
// Update the camera's orientation
this.entity.setEulerAngles(this.ex, this.ey, 0);
if (app.keyboard.isPressed(pc.KEY_E)) {
SPEED = 30;
} else {
SPEED = 15;
}
// Update the camera's position
if (app.keyboard.isPressed(pc.KEY_W)) {
this.entity.translateLocal(0, 0, -SPEED*dt);
}
if (app.keyboard.isPressed(pc.KEY_S)) {
this.entity.translateLocal(0, 0, SPEED*dt);
}
if (app.keyboard.isPressed(pc.KEY_A)) {
this.entity.translateLocal(-SPEED*dt, 0, 0);
} else if (app.keyboard.isPressed(pc.KEY_D)) {
this.entity.translateLocal(SPEED*dt, 0, 0);
}
if (app.keyboard.isPressed(pc.KEY_C)) {
this.entity.translateLocal(0, -SPEED*dt, 0);
} else if (app.keyboard.isPressed(pc.KEY_SPACE)) {
this.entity.translateLocal(0, SPEED*dt, 0);
}
},
onMouseMove: function (event) {
// Update the current Euler angles, clamp the pitch.
this.ex -= event.dy / 5;
this.ex = pc.math.clamp(this.ex, -30, 30);
this.ey -= event.dx / 5;
},
onMouseDown: function (event) {
// When the mouse button is clicked try and capture the pointer
if (!pc.Mouse.isPointerLocked()) {
app.mouse.enablePointerLock();
}
},
};
return Player;
});
Would anyone be willing to help me out, or point me in the direction of examples? I’ve looked at the api for the rigidbody stuff, but tbh, I’ve always found api documentation quite confusing. :-/
Thanks
TheMightySpud