[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