[SOLVED] How to detect a collision with the player entity

In this script I need help with a script that involves an entity touching another. The script is going to basically make the entity go to a certain position if touching another entity someone please help.

Hi @Mason_Rocchio!

Can you explain this a little better please? Do you have a project link that you can share?

Ok so, if a entity in my game is touching the player. it will move my player to a giving quarantine

You can use different ways, for example setPosition() and teleport(), but it depends on the components you use on the entity.

Yeah I know, I am still having trouble with collisions.
I don’t know how to write scripts that if touching entity 1, go to 2,5,4
for example.

The page below gives all information you need. Please share your project if you get stuck.

https://developer.playcanvas.com/en/tutorials/collision-and-triggers/

Ok thank you

[https://launch.playcanvas.com/1311766?debug=true

This is the link, if you touch the red guy you are supposed to go back to the spawnpoint where you start in the beginning.

Try to add this script to your player entity.

var Teleport = pc.createScript('teleport');

// initialize code called once per entity
Teleport.prototype.initialize = function () {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};

Teleport.prototype.onCollisionStart = function (result) {
    if (result.other.parent.parent.name === 'Neighbor') {
        this.entity.rigidbody.teleport(new pc.Vec3(0,0,0));
    }
};
1 Like

When I copied the script and started the game it gave this error

ASSERT FAILED:

Invalid batch 100000 insertion

ASSERT FAILED:

Invalid batch 100000 insertion

[caught.js?id=65474609&branchId=ba541ccb-17a2-4cee-b087-061be2d7c60e:5]: Teleport is not defined

ReferenceError: Teleport is not defined
at scriptType.Caught.initialize (https://launch.playcanvas.com/api/assets/files/caught.js?id=65474609&branchId=ba541ccb-17a2-4cee-b087-061be2d7c60e:5:5)
at ScriptComponent._scriptMethod (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.51.5:64102:19)
at ScriptComponent._onInitialize (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.51.5:64126:34)
at ScriptComponentSystem._callComponentMethod (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.51.5:64712:49)
at ScriptComponentSystem._onInitialize (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.51.5:64721:9)
at ComponentSystemRegistry.fire (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.51.5:758:18)
at Application.start (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.51.5:71225:17)
at https://launch.playcanvas.com/editor/scene/js/launch.js:14548:25
at Application._preloadScripts (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.51.5:70970:5)
at https://launch.playcanvas.com/editor/scene/js/launch.js:14531:21

Have you created a script with the name teleport and replaced the code with the code above?

This part is not related and has another cause.

1 Like

I just took the code part and pasted it under the initialize function

Ok yeah you were right I did the wrong code.

Thanks

1 Like

GameManager.prototype.enterStartState = function () {
console.log(“We are in the Start State”);
//Show many things in one then
return Promise.resolve()

    .then(() => {
    // Play the baby animation and wait for 1500 milliseconds
    const doctorPromise = doctor.enabled = true;
    this.subtitleTextMain.enabled = true;
    const delayPromise = new Promise(resolve => setTimeout(resolve, 1000));
    // Race between the baby animation and the delay
    return Promise.race([doctorPromise, delayPromise])
        .then(() => {
            // Play the mother animation after the delay
            return this.playAudioWithAnimationPromise(doctor, 'Talk1', 'Doc_Aud_1', doctorDialogue[0]);
        });
    })
    .then(() => this.tablePointer.enabled = true)
    .then(() => this.waitForPlayerToHitTable('playerHitTable'))
    .then(() => this.tablePointer.enabled = false)
    .then(() => this.which_grabHints(2))
    .then(() => {
        // Start executing these lines concurrently with the 2500ms delay
        const buttonPromise = this.which_Button(0);
        const waitForButtonPromise = this.waitForButtonClicked('isGetStartedButton_Clicked');

        return Promise.all([
        new Promise(resolve => setTimeout(resolve, 4500)),
        buttonPromise,
        waitForButtonPromise,
        ]);
    })
    .then(() => this.full_GrabHints(false))
    .then(() => this.bedwithPillowFULL(false))
    .then(() => this.theEntityHolder.bedWithoutPillow.enabled = true)
    .then(() => {
        this.htmlcssLoaderScript.fadehtml.style.display = "block";
        return new Promise(resolve => setTimeout(resolve, 2000));
    })
    .then(() => this.htmlcssLoaderScript.fadehtml.style.display = "none")
    .then(() => this.charactersLoad())
    .then(() => this.crosshairLoaderScript.screens[0].enabled = false)
    .then(() => this.which_Button(1))
    .then(() => this.changeState(GameState.AnimState))
    .catch((error) => {
        console.error('Error:', error);
    });

};

I get this error after this line:
return this.playAudioWithAnimationPromise(doctor, ‘Talk1’, ‘Doc_Aud_1’, doctorDialogue[0]);

Actually this game is made for FPS controls for PC with the same code which works so perfect
I am making a VR version. but the same gameplay flow is crashing with this error

ASSERT FAILED:

Invalid batch 100000 insertion

ASSERT FAILED:

Invalid batch 100000 insertion

Please help me fix it
I tried all possible ways from my side
Please suggest me the correct and working code without this error

1 Like