Helloo dear Community,
Now I spend the whole evening to debug my code and finaly found the problem. Its easy but i dont get it.
I recreated the problem in a fresh “Basic Mouse Input” project-fork. And added just 2 lines of code;
this.entity.model.enabled=false;
and
this.entity.script.enabled=false;
https://playcanv.as/p/U9p9Z0av/
You will see that you can disable the Mesh-Model component with the left click but you can’t disable the Script component with the right click. Why is this so?
var Mouse = pc.createScript('mouse');
Mouse.attributes.add('redMaterial', {
type: 'asset',
assetType: 'material'
});
Mouse.attributes.add('greenMaterial', {
type: 'asset',
assetType: 'material'
});
Mouse.attributes.add('blueMaterial', {
type: 'asset',
assetType: 'material'
});
// initialize code called once per entity
Mouse.prototype.initialize = function() {
this.pos = new pc.Vec3();
// Disabling the context menu stops the browser displaying a menu when
// you right-click the page
this.app.mouse.disableContextMenu();
// Use the on() method to attach event handlers.
// The mouse object supports events on move, button down and
// up, and scroll wheel.
this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
};
Mouse.prototype.onMouseMove = function (event) {
// Use the camera component's screenToWorld function to convert the
// position of the mouse into a position in 3D space
var depth = 10;
var cameraEntity = this.app.root.findByName('Camera');
cameraEntity.camera.screenToWorld(event.x, event.y, depth, this.pos);
// Finally update the cube's world-space position
this.entity.setPosition(this.pos);
};
Mouse.prototype.onMouseDown = function (event) {
// If the left mouse button is pressed, change the cube color to red
if (event.button === pc.MOUSEBUTTON_LEFT) {
this.entity.model.meshInstances[0].material = this.redMaterial.resource;
/////////////---------DISABLE MESH WORKS--------------//////////////////////////////
this.entity.model.enabled=false;
}
// If the left mouse button is pressed, change the cube color to green
if (event.button === pc.MOUSEBUTTON_MIDDLE) {
this.entity.model.meshInstances[0].material = this.greenMaterial.resource;
}
// If the left mouse button is pressed, change the cube color to blue
if (event.button === pc.MOUSEBUTTON_RIGHT) {
this.entity.model.meshInstances[0].material = this.blueMaterial.resource;
////////////-------DISABLE SCRIPT DONT WORK-------------//////////////////////////////
this.entity.script.enabled=false;
}
};