Controller.prototype.onTriggerEnter = function(result){
if(result.other.tags.has('player') && this.count==2 )
{
this.app.fire("rotator:point");
this.other.entity.destroy();
this.count =4;
this.countText.element.text = +this.count;
}
};
When player tag and counter are 2, I want to get the object and destroy the object but it doesn’t work
Where is the problem?
Hi @Ozden_Delibas,
Have you debugged what is the value of this.count
? Is it 2 as you expect or is it something else?
Try sharing some additional code on how you set the value for that property.
var Controller = pc.createScript('controller');
Controller.attributes.add('speed', {
type: 'number',
default: 3
});
Controller.attributes.add('direction_speed',{
type: 'number',
default: 17
});
Controller.attributes.add('count',{
type: 'number',
default: 2
});
Controller.attributes.add('countText',{
type: 'entity'
});
Controller.attributes.add('winText',{
type: 'entity'
});
Controller.attributes.add("sceneId",{
type:"string",
default: "0",
title: "New Level"
});
// initialize code called once per entity
Controller.prototype.initialize = function() {
//this.entity.collision.on('collisionstart', this.onCollisionStart, this);
this.app.off("rotator:point");
this.app.on("rotator:point", this.onPoint, this);
this.countText.element.text = "Count: 2";
this.winText.element.text = "";
this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
this.sagButton = this.app.root.findByName('SagButton');
this.solButton = this.app.root.findByName('SolButtton');
// buttons state
this.isSagPressed = false;
this.isSolPressed = false;
// remove old event listenrs
this.sagButton.element.off('mousedown');
this.sagButton.element.off('mouseup');
this.sagButton.element.off('touchstart');
this.sagButton.element.off('touchend');
this.solButton.element.off('mousedown');
this.solButton.element.off('mouseup');
this.solButton.element.off('touchstart');
this.solButton.element.off('touchend');
//add new event listeners
this.sagButton.element.on('mousedown', this.onSagDown, this);
this.sagButton.element.on('mouseup', this.onSagUp, this);
this.sagButton.element.on('touchstart', this.onSagDown, this);
this.sagButton.element.on('touchend', this.onSagUp, this);
this.solButton.element.on('mousedown', this.onSolDown, this);
this.solButton.element.on('mouseup', this.onSolUp, this);
this.solButton.element.on('touchstart', this.onSagDown, this);
this.solButton.element.on('touchend', this.onSagUp, this);
};
Controller.prototype.onSagDown = function(event){
this.isSagPressed = true;
};
Controller.prototype.onSagUp = function(event){
this.isSagPressed = false;
};
Controller.prototype.onSolDown = function(event){
this.isSolPressed = true;
};
Controller.prototype.onSolUp = function(event){
this.isSolPressed = false;
};
Controller.prototype.onCollisionStart = function(result){
if(result.other.rigidbody.entity.tags.has('deneme')){
this.entity.destroy();
}
};
// update code called every frame
Controller.prototype.update = function(dt) {
var rb = this.entity.rigidbody;
var direction_rb = this.entity.rigidbody;
var moveHoriz = 0;
var moveVert = 0;
var direction_moveHoriz = 0;
var direction_moveVert = 0;
if(this.app.keyboard.isPressed(pc.KEY_LEFT) || this.isSolPressed){
direction_moveHoriz = 1;
}
if(this.app.keyboard.isPressed(pc.KEY_RIGHT) || this.isSagPressed){
direction_moveHoriz = -1;
}
if(this.speed <= 3){
moveVert = -1;
} else this.speed = 3;
var moveVect = new pc.Vec3(moveVert, 0, moveHoriz);
rb.applyForce(moveVect.scale(this.speed));
var direction_moveVect = new pc.Vec3(direction_moveVert, 0, direction_moveHoriz);
direction_rb.applyForce(direction_moveVect.scale(this.direction_speed));
};
Controller.prototype.onTriggerEnter = function(result){
if(result.other.tags.has('player') && this.count==2 )
{
this.app.fire("rotator:point");
this.other.entity.destroy();
this.count =4;
this.countText.element.text = +this.count;
}
if(result.other.tags.has('player') && this.count==4){
this.app.fire("rotator:point");
this.other.destroy();
this.count =8;
}
if(result.other.tags.has('player') && this.count==8){
this.app.fire("rotator:point");
this.other.destroy();
this.count =16;
}
if(result.other.tags.has('player')&& this.count==16){
this.app.fire("rotator:point");
this.other.destroy();
this.count =32;
}
if(result.other.tags.has('player') &&this.count==32){
this.app.fire("rotator:point");
this.other.destroy();
this.count =64;
}
if(result.other.tags.has('player')&&this.count==64){
this.app.fire("rotator:point");
this.other.destroy();
}
};
onTriggerEnter doesn’t work at all
On what kind of entities do you have this script attached to?
For triggerenter
to work the entity needs to have a collision component, but no rigidbody component. Start with this example that showcases how to add a trigger and use triggerenter
event:
https://developer.playcanvas.com/en/tutorials/collision-and-triggers/
1 Like
I think the main problem here is that you are trying to enter into the ‘other’ property of the rigidbody entering the trigger. Because it is a trigger volume only, it is assumed that the result will be the item entering the volume space.
Try making the following change:
if(result.tags.has('player') && this.count==2 )
{
this.app.fire("rotator:point");
this.other.entity.destroy();
this.count =4;
this.countText.element.text = +this.count;
}
Here is the API reference for your review:
https://developer.playcanvas.com/en/api/pc.CollisionComponent.html#event:triggerenter
I hope this is helpful.
3 Likes
Hi @Ozden_Delibas! In addition to what already has been mentioned, I think the rule above is incorrect. Probably you want to use result.destroy();
or this.entity.destroy();
.
1 Like