https://playcanvas.com/project/1004154/overview/300-am-at-mr-greens
I want to make it where when I am near an entity I can press e to cause an event like pick up an item or open a passage. Any ideas or scripts would be helpful.
https://playcanvas.com/project/1004154/overview/300-am-at-mr-greens
I want to make it where when I am near an entity I can press e to cause an event like pick up an item or open a passage. Any ideas or scripts would be helpful.
@Brent_Reddick I took a look at what you have so far and it looks great. I have made a little demo code to do this. Actually there are many ways and you can look at some more forum posts if you would like. Here is my example.
https://playcanvas.com/project/939691/overview/spacedoors
In fps-controller script I look for E
// // Process Action keys
if(app.keyboard.wasPressed(pc.KEY_E)) {
this.processRaycast();
}
Process a raycast
FpsController.prototype.processRaycast = function() {
var centerScreenX = this.app.graphicsDevice.width / 2;
var centerScreenY = this.app.graphicsDevice.height / 2;
var start = this.camera.camera.screenToWorld(centerScreenX, centerScreenY, this.camera.camera.nearClip);
var end = this.camera.camera.screenToWorld(centerScreenX, centerScreenY, this.camera.camera.farClip);
// raycast between the two points and return the closest hit result
var result = this.app.systems.rigidbody.raycastFirst(start, end);
if(result) {
var pickEntity = result.entity;
if(pickEntity !== null) {
console.log(pickEntity.name);
if(!result.entity.tags.has('double')) {
// Execute Activation
pickEntity.fire("activate");
}
} else {
console.log("Nothing Hit");
}
} else {
console.log("Open Space and entity is NULL");
}
};
I put listeners in the function for each item that has some type of action and then use the .fire to signal the action.