Collision Pop ups

Hi! I’m very new to playcanvas. I’m making a 2D top-down game for a class. This is what I’m hoping to accomplish: when my character goes to an object/npc (ex:"aeta), an entity would pop up. But the template script I was given is not working, nothing is appearing. They both have collision components. Can anyone help me?


var ArtifactCollisions = pc.createScript('artifactCollisions');

ArtifactCollisions.attributes.add('aetasOneSprite', {
    type: 'entity',
    title: 'Aetas One Sprite',
    description: 'The entity representing the sprite named "aetas-one".'
});

ArtifactCollisions.attributes.add('aetasOneButton', {
    type: 'entity',
    title: 'Aetas One Button',
    description: 'The button entity named "aetas-one-button".'
});

ArtifactCollisions.attributes.add('aetasTwoSprite', {
    type: 'entity',
    title: 'Aetas Two Sprite',
    description: 'The entity representing the sprite named "aetas-two".'
});

ArtifactCollisions.attributes.add('aetasTwoButton', {
    type: 'entity',
    title: 'Aetas Two Button',
    description: 'The button entity named "aetas-two-button".'
});

ArtifactCollisions.prototype.initialize = function() {
    var self = this;

    if (!this.aetasOneSprite || !this.aetasOneButton || !this.aetasTwoSprite || !this.aetasTwoButton) {
        console.error("One or more entities are not defined.");
        return;
    }

    // Hide all sprites and buttons initially
    this.hideArtifactEntities();

    // Check for collision start events
    this.entity.collision.on('triggerenter', function (result) {
        self.onTriggerEnter(result);
    });
};

ArtifactCollisions.prototype.onTriggerEnter = function(result) {
    if (!result || !result.other || !result.other.entity) {
        return;
    }

    var collidedEntity = result.other.entity;

    // Check if collided entity is named "aeta"
    if (collidedEntity.name === 'aeta') {
        // Show the first sprite and button
        this.showArtifactOne();
    }
};

ArtifactCollisions.prototype.showArtifactOne = function() {
    // Show the first sprite and button
    this.aetasOneSprite.enabled = true;
    this.aetasOneButton.enabled = true;

    // Add click event listener to the first button
    var self = this;
    this.aetasOneButton.element.on('click', function (event) {
        self.handleAetasOneButtonClick();
    });
};

ArtifactCollisions.prototype.handleAetasOneButtonClick = function() {
    // Hide the first sprite and button
    this.aetasOneSprite.enabled = false;
    this.aetasOneButton.enabled = false;

    // Show the second sprite and button
    this.showArtifactTwo();
};

ArtifactCollisions.prototype.showArtifactTwo = function() {
    // Show the second sprite and button
    this.aetasTwoSprite.enabled = true;
    this.aetasTwoButton.enabled = true;

    // Add click event listener to the second button
    var self = this;
    this.aetasTwoButton.element.on('click', function (event) {
        self.handleAetasTwoButtonClick();
    });
};

ArtifactCollisions.prototype.handleAetasTwoButtonClick = function() {
    // Hide the second sprite and button
    this.aetasTwoSprite.enabled = false;
    this.aetasTwoButton.enabled = false;
};

ArtifactCollisions.prototype.hideArtifactEntities = function() {
    // Hide all sprites and buttons
    this.aetasOneSprite.enabled = false;
    this.aetasOneButton.enabled = false;
    this.aetasTwoSprite.enabled = false;
    this.aetasTwoButton.enabled = false;
};

Hi @Jasmin_Gonzales and welcome!

Please make sure the trigger entity has no rigidbody component and the other entity has also a rigidbody component to activate the trigger.

Also don’t forget to assign the entities to the attributes of the script.

Thank you for the response! entities are assigned and rigidbody components are assigned only to the character. Still, nothing is showing up when I touch the object.

Is the type of the rigidbody correct? If you move the entity by forces it has to be dynamic, otherwise it has to be kinematic.

Also check if all entities have the correct name to match with the script. Looking at line 52, if the script is attached to the aeta the collidedEntity should be the player?