I am trying to pick an object using Collisioning using the code below:
Gamemanager.attributes.add("Camera", {type: "entity"});
// initialize code called once per entity
Gamemanager.prototype.initialize = function() {
//Look for mouse down event
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onSelect, this);
};
Gamemanager.prototype.onSelect = function(e){
if(canSelect){
var from = this.Camera.entity.camera.screenToWorld(e.x, e.y, this.Camera.entity.camera.nearClip);
var to = this.Camera.entity.camera.screenToWorld(e.x, e.y, this.Camera.entity.camera.farClip);
var result = this.app.systems.rigidbody.raycastFirst(from, to);
if (result) {
var pickedEntity = result.entity;
console.log(pickedEntity.name);
//pickedEntity.script.pulse.pulse();
}
}
};
I have already parsed the script and added the camera in the editor. For completeness, I have also added a collision component to the object being clicked. When i click on the object, I get the error code Cannot read property ‘entity’ of null. Any ideas?
this.Camera is already an entity, no need to use .entity again:
var from = this.Camera.camera.screenToWorld(e.x, e.y, this.Camera.camera.nearClip);
var to = this.Camera.camera.screenToWorld(e.x, e.y, this.Camera.camera.farClip);
I see you use this.thisCamera in your script instead of just thisCamera. If you want to use this.thisCamera you have to add this.thisCamera = this.entity.camera; to your initialize function.
yeah Im just trying to see whats wrong. I just removed the this and its still throwing the null reference.
Gamemanager.prototype.onSelect = function(e){
if(canSelect){
var from = thisCamera.camera.screenToWorld(e.x, e.y, thisCamera.camera.nearClip);
var to = thisCamera.camera.screenToWorld(e.x, e.y, thisCamera.camera.farClip);
var result = this.app.systems.rigidbody.raycastFirst(from, to);
if (result) {
var pickedEntity = result.entity;
console.log(pickedEntity.name);
}
}
};
Additionally, I added the `this.thisCamera = this.entity.camera; to initialize and created a var thisCamera and then changed the code to
Gamemanager.prototype.onSelect = function(e){
if(canSelect){
var from = this.thisCamera.camera.screenToWorld(e.x, e.y, this.thisCamera.camera.nearClip);
var to = this.thisCamera.camera.screenToWorld(e.x, e.y, this.thisCamera.camera.farClip);
var result = this.app.systems.rigidbody.raycastFirst(from, to);
if (result) {
var pickedEntity = result.entity;
console.log(pickedEntity.name);
//pickedEntity.script.pulse.pulse();
}
}
};
Probably because thisCamera is already your camera component. Right now you try to get the camera component of the camera component and that result is null.
Alright alright, I think I’m getting closer. After removing the superfluous camera and now having this
Gamemanager.prototype.onSelect = function(e){
if(canSelect){
var from = this.thisCamera.screenToWorld(e.x, e.y, this.thisCamera.nearClip);
var to = this.thisCamera.screenToWorld(e.x, e.y, this.thisCamera.farClip);
var result = this.app.systems.rigidbody.raycastFirst(from, to);
if (result) {
var pickedEntity = result.entity;
console.log(pickedEntity.name);
//pickedEntity.script.pulse.pulse();
}
}
};
I get the error Cannot read property ‘screenToWorld’ of null
//Create an attribute to make a reference to the camera
Gamemanager.attributes.add("thisCamera", {type: "entity"});
Nothing in initialize.
and the script is
Gamemanager.prototype.onSelect = function(e){
if(canSelect){
var from = thisCamera.screenToWorld(e.x, e.y, thisCamera.nearClip);
var to = thisCamera.screenToWorld(e.x, e.y, thisCamera.farClip);
var result = this.app.systems.rigidbody.raycastFirst(from, to);
if (result) {
var pickedEntity = result.entity;
console.log(pickedEntity.name);
//pickedEntity.script.pulse.pulse();
}
}
};
I get the error code: thisCamera is not defined. But I shouldnt need to declare it in initialize if I have it in a sciptable attribute right?
Gamemanager.prototype.onSelect = function(e){
if(canSelect){
var from = this.thisCamera.camera.screenToWorld(e.x, e.y, this.thisCamera.camera.nearClip);
var to = this.thisCamera.camera.screenToWorld(e.x, e.y, this.thisCamera.camera.farClip);
var result = this.app.systems.rigidbody.raycastFirst(from, to);
if (result) {
var pickedEntity = result.entity;
console.log(pickedEntity.name);
//pickedEntity.script.pulse.pulse();
}
}
};
I parsed and made sure the camera is in there. I still get: Uncaught TypeError: Cannot read property ‘camera’ of null.
Your set up seems to be correct right now. I see the first time it’s also working without any error, only after the first time there is a problem. What do you do when you move the cubes?