How do I teleport an entity?

var Player2Controller = pc.createScript('player2Controller');

// initialize code called once per entity
Player2Controller.prototype.initialize = function() {
    var movePlayer2 = function(x, y, z){
        this.entity.rigidbody.teleport(x, y, z);
    };"player:moved", movePlayer2);

// update code called every frame
Player2Controller.prototype.update = function(dt) {


This script I am trying to use to move a second player in a multiplayer game I am making. However, when I run this code with it attached to my player 2 entity, it gives me the following error:
[Player2Controller.js?id=61323218&branchId=60559a1c-eaaf-4b90-9e12-c32f402dafee:6]: Uncaught TypeError: Cannot read properties of undefined (reading ‘rigidbody’)

TypeError: Cannot read properties of undefined (reading ‘rigidbody’)
at Application.movePlayer2 (
at (
at Socket. (
at Socket.Emitter.emit (
at Socket.emitEvent (
at Socket.onevent (
at Socket.onpacket (
at Manager.Emitter.emit (
at Manager.ondecoded (
at Decoder.Emitter.emit (

Here is my project link:

I have moved entities with this line of code before, and I just can’t figure out what I am missing this time… Any help would be greatly appreciated!

Hi there Nathan_Crouch

The error you are getting says there’s no “entity” defined in the context of the function call.

Try adding “this” as the last parameter for the listener."player:moved", movePlayer2, this);

The final “this” sets the context (also known as scope) for the callback function