Really quick syntax question

Hello! I am trying to create a script that teleports an entity named “Player” on collision. The collision code is correct. But my teleporting code is not. Now, I am trying to teleport a variable called “player,” and I am not sure how to do so. I simply tried to stick the variable name on the front of a function, but that results in an error saying “Player is not defined.” Here is my code:

var Trigger = pc.createScript('trigger');


// initialize code called once per entity
Trigger.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
};

//this code is the code not working, supposed to execute when something collides with this entity.
Trigger.prototype.onTriggerEnter = function(entity) {
    console.log(entity);
    entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
    entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
    // display a message after collision via console.
    Player.entity.rigidbody.teleport(-1.311, 0.5, position.z + 12);
    
    console.log("Collided With Player");
};

Trigger.prototype.update = function(dt) {
    var Player = this.app.root.findByName('Player');
};

Obviously I was trying something I do not know well, and this does not work, but if anyone could tell me how to accomplish this task or at least point me in the right direction, I will be very thankful!

Hi @CRAYPHiSHQUESTIONS,

This is happening because the Player variable is outside the scope which you are calling it in. If you study your code carefully, you have defined player in the update method, while you are trying to use the variable in the onTriggerEnter method. To fix this, you could assign the player entity to an object defined in the initialize method. update is called every frame, and since the value of Player does not change, there is no need to re-declare every frame. Another thing that I noticed was that you are calling Player.entity.rigidbody, this is not required as the findByName function returns the entity. The following code should solve your issue -

var Trigger = pc.createScript('trigger');


// initialize code called once per entity
Trigger.prototype.initialize = function() {
    this.player = this.app.root.findByName('Player');
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
};

//this code is the code not working, supposed to execute when something collides with this entity.
Trigger.prototype.onTriggerEnter = function(entity) {
    console.log(entity);
    entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
    entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
    // display a message after collision via console.
    this.player.rigidbody.teleport(-1.311, 0.5, position.z + 12);
    
    console.log("Collided With Player");
};

Trigger.prototype.update = function(dt) {
};

I would strongly recommend reading a bit more about scope in JavaScript, this article is a good place to start.

Hope this helps @CRAYPHiSHQUESTIONS!

2 Likes

Thank you so much! I will read that!