Destoying from scene shows all children deleted, but still childrens are visible in scene on collision : -
On collision with the saw, this is happening : -
onLoad() {
this.animationComponent = this.node.getComponent(Animation);
this.animationComponent.clips[0].name = AnimationNames.IDLE;
this.animationComponent.clips[1].name = AnimationNames.RUN;
this.animationComponent.clips[2].name = AnimationNames.DANCE_1;
this.animationComponent.clips[3].name = AnimationNames.DANCE_2;
this.characterBody.getComponent(RigidBody).useCCD = true;
this.characterBody
.getComponent(BoxCollider)
.on("onCollisionEnter", this.onCollisionEnter, this);
}
onCollisionEnter(event: ICollisionEvent) {
const otherColliderName = event.otherCollider.name;
const selfColliderName = event.selfCollider.name;
if (otherColliderName.includes("Ground-Mesh")) {
// Handle Ground-Mesh collision
} else if (otherColliderName.includes("ramp")) {
// Handle ramp collision
} else if (otherColliderName.includes("saw")) {
console.log("saw", this.node.getParent().children);
this.node
.getParent()
.getComponent(CharactersManager)
.removeCharacter(this.node); // Ensure you pass the correct node
} else if (otherColliderName.includes("knife")) {
// Handle knife collision
} else if (otherColliderName.includes("hammer")) {
// Handle hammer collision
} else if (otherColliderName.includes("LeftSide")) {
// Handle LeftSide collision
} else if (otherColliderName.includes("RightSide")) {
// Handle RightSide collision
}
}
From this, call is going to the parent i.e. -
removeCharacter(character: Node) {
const characterUUID = character.uuid;
let characterToBeRemoved = null;
// Find the character to be removed
for (let i = 0; i < this.characters.length; i++) {
if (this.characters[i].uuid === characterUUID) {
characterToBeRemoved = { node: this.characters[i], index: i };
break; // Exit the loop once the character is found
}
}
// Remove the character if found
if (characterToBeRemoved) {
const nodeToRemove = characterToBeRemoved.node;
// Stop all animations on the node
const animationComponent = nodeToRemove.getComponent(Animation);
if (animationComponent) {
animationComponent.stop();
}
// Destroy the character node
nodeToRemove.destroy();
// Remove from characters array
this.characters.splice(characterToBeRemoved.index, 1);
// Update children count
this.charactersCurrentlyInScene--;
// Remove from parent node's children array
if (nodeToRemove.parent) {
const indexInChildren =
nodeToRemove.parent.children.indexOf(nodeToRemove);
if (indexInChildren !== -1) {
nodeToRemove.parent.children.splice(indexInChildren, 1);
}
}
}
}
At the end, complete array is consoled as empty array, but childrens are still visible in the scene. What’s happening wrong with the collision detection code? How can i fix this ?