How can I make the character only jump when it is touching something with the tag ‘solid’ and preferably only on a vertical collision?
var PlayerController = pc.createScript('playerController');
PlayerController.attributes.add('power', { type: 'number' });
PlayerController.attributes.add('jumpHeight', { type: 'number' });
PlayerController.attributes.add('modelEntity', { type: 'entity' });
PlayerController.prototype.initialize = function() {
this._anim = this.modelEntity.anim;
this._angle = 0;
this.baseAngle = 0;
this.yVel = 0;
};
PlayerController.prototype.update = function(dt) {
var x = 0;
var z = 0;
if (this.yVel > 0) {
this.yVel -= 3;
}
if (this.app.keyboard.isPressed(pc.KEY_W)) {
x -= 1;
}
if (this.app.keyboard.isPressed(pc.KEY_S)) {
x += 1;
}
if (this.app.keyboard.isPressed(pc.KEY_A)) {
z -= 1;
}
if (this.app.keyboard.isPressed(pc.KEY_D)) {
z += 1;
}
/*if (this.app.keyboard.isPressed(pc.KEY_LEFT)) {
this.baseAngle += 10;
this.fire('base angle', this.baseAngle);
}
if (this.app.keyboard.isPressed(pc.KEY_RIGHT)) {
this.baseAngle -= 10;
this.fire('base angle', this.baseAngle);
}*/
if (this.app.keyboard.wasPressed(pc.KEY_SPACE) && this.entity.rigidbody.linearVelocity.y === 0) {
this.yVel = this.jumpHeight;
}
this.entity.rigidbody.applyForce(0, this.yVel, 0);
let movement = new pc.Vec3(-x * Math.sin(this.baseAngle * Math.PI / 180) - z * Math.cos(this.baseAngle * Math.PI / 180), 0, -x * Math.cos(this.baseAngle * Math.PI / 180) - z * Math.sin(this.baseAngle * Math.PI / 180)).normalize().scale(dt * this.power);
this.entity.rigidbody.applyForce(movement);
if(x !== 0 || z !== 0) {
this._angle = pc.math.lerpAngle(this._angle, 180 + (Math.atan2(z, x) * pc.math.RAD_TO_DEG), 0.4) % 360;
this.modelEntity.setEulerAngles(0, this._angle, 0);
}
else {
this._angle = pc.math.lerpAngle(this._angle, this.baseAngle, 0.4) % 360;
this.modelEntity.setEulerAngles(0, this._angle, 0);
}
this.modelEntity.anim.setFloat('Speed', Math.abs(x+z));
this.modelEntity.anim.setFloat('Jump', this.yVel > 0 ? 1 : 0);
};