I have a problem with this.raycastPlayerBase.getPosition ()
and I get the following error “Cannot read property ‘getPosition’ of null”, perform a console log of raycastPlayerBase and it returns me null, but I had already defined it at the beginning.
I am quite a newbie and the error is probably simple, I apologize for that.
I leave the complete code here:
var PlayerMovement = pc.createScript('playerMovement');
PlayerMovement.attributes.add('speed', { type: 'number', default: 0.09 });
PlayerMovement.attributes.add('movementMultiplier', {
type: 'number',
default: 2.0
});
PlayerMovement.attributes.add('jumpPower', {
type: 'number',
default: 200.0
});
PlayerMovement.attributes.add('raycastPlayerBase', {
type: 'entity'
});
PlayerMovement.prototype.initialize = function () {
var app = this.app;
var camera = app.root.findByName('Camera');
this.cameraScript = camera.script.cameraMovement;
this.force = new pc.Vec3();
this.jumping = {
state: false
};
this.running = {
state: false
};
};
PlayerMovement.prototype.update = function (dt) {
var app = this.app;
var force = this.force;
var forward = this.entity.forward;
var right = this.entity.right;
var x = 0;
var z = 0;
if (app.keyboard.isPressed(pc.KEY_A)) {
x -= right.x;
z -= right.z;
}
if (app.keyboard.isPressed(pc.KEY_D)) {
x += right.x;
z += right.z;
}
if (app.keyboard.isPressed(pc.KEY_W)) {
x += forward.x;
z += forward.z;
}
if (app.keyboard.isPressed(pc.KEY_S)) {
x -= forward.x;
z -= forward.z;
}
if (app.keyboard.isPressed(pc.KEY_SHIFT)) {
this.running.state = true;
} else {
this.running.state = false;
}
if (app.keyboard.wasPressed(pc.KEY_SPACE)) {
if (this.jumping.state === false) {
this.entity.rigidbody.applyImpulse(0, this.jumpPower, 0);
this.jumping.state = true;
}
} else if (this.jumping.state === true) {
if (this._checkBelow() !== null) {
this.jumping.state = false;
}
}
if (x !== 0 || z !== 0) {
var pos = new pc.Vec3(x * dt, 0, z * dt);
pos.normalize().scale(this.speed);
pos.add(this.entity.getPosition());
var targetY = this.cameraScript.eulers.x + 180;
var rot = new pc.Vec3(0, targetY, 0);
this.entity.rigidbody.teleport(pos, rot);
if (this.running.state === true) {
force.set(x, 0, z).normalize().scale(this.speed * this.movementMultiplier);
} else {
force.set(x, 0, z).normalize().scale(this.speed);
}
this.entity.translate(force);
this.entity.rigidbody.applyForce(force);
this.entity.rigidbody.syncEntityToBody();
}
};
PlayerMovement.prototype._checkBelow = function() {
return this.app.systems.rigidbody.raycastFirst(this.entity.getPosition(), this.raycastPlayerBase.getPosition());
};
From already thank you very much