Hello all,
I’ve gotten off to a great start with Play Canvas and its an awesome engine so far! I’m learning how to register a click on an entity and call a function when that happens. I’ve got the code from the picker_raycast tutorial, but I cannot seem to detect when I click on a specific entity. Here is how I have everything setup:
-
I have a model that when users click on a specific area a function will be called. I don’t want a click on anywhere on the model to registers a click, just a specific part.
-
I define that specific part by using a blank entity with a rigid body and collision (set extents to cover the area I am interested in) component. The script to carry out the action is attached to the blank entity. See below:
pc.script.create(‘picker_raycast’, function (app) {
// Creates a new PickerRaycast instance
var PickerRaycast = function (entity) {
this.entity = entity;
};PickerRaycast.prototype = { // Called once after all resources are loaded and before the first update initialize: function () { app.mouse.on(pc.input.EVENT_MOUSEDOWN, this.onSelect, this); }, onSelect: function (e) { var from = this.entity.getPosition(); var to = this.entity.getParent().findByPath("Camera").camera.screenToWorld(e.x, e.y, this.entity.getParent().findByPath("Camera").camera.farClip); console.log("ding"); app.systems.rigidbody.raycastFirst(from, to, function (result) { var pickedEntity = result.entity; alert("boo"); }.bind(this)); }, // Called every frame, dt is time in seconds since last update update: function (dt) { } }; return PickerRaycast;
});