[SOLVED] Other player rotation not seen by player

Can you post your network script here so I can test it?

You can fork the project and use everything you need. Don’t forget to add the lookAtMouse.js script to your player entity.

I already have it. the look at mouse script anyway.

Hey, how would I make a random spawn point for my player when they join the server?

Can we first handle this problem? Please keep every problem that’s not related in it’s own topic.

Understood sir.

@Albertos can you add the rotation script to my network.js if I post it here?

I see in your project that you already did that yourself.

1 Like

But it does not work why? Did I do it wrong?
Here is the editor: https://playcanvas.com/editor/scene/1338583

You can’t see if it works because you have some errors. You have to solve these errors first. If this is because the new code you probably have forget to add something from your old code.

1 Like

Okay heres my script:

var Network = pc.createScript('network');

Network.attributes.add('gamePlayManagerEntity', {type: 'entity'});


// static variables
Network.id = null;
Network.socket = null;

// initialize code called once per entity
Network.prototype.initialize = function() {
    // this.player = this.app.root.findByName('Player');
    // this.other = this.app.root.findByName('Other');
    
    this.player = this.gamePlayManagerEntity.script.gamePlayManagerScript.playerEntity;
    this.other = this.gamePlayManagerEntity.script.gamePlayManagerScript.otherEntity;
    
    // var socket = io.connect('https://playcanvas-multiplayer.glitch.me'); // Glitch hosted server
    var socket = io.connect('https://playcanvas-mp.glitch.me'); // Glitch hosted server
    Network.socket = socket;
    
    // socket.emit ('initialize');
    var nicknameVal = this.player.findByName('playerNickText').element.text;
    socket.emit ('initialize',{nicknameVal: nicknameVal});
    
    // this.displaySendMsg();
    
    var self = this;
    socket.on ('playerData', function (data) {
        self.initializePlayers (data);
    });

    socket.on ('playerJoined', function (data) {
        self.addPlayer(data);
    });

    socket.on ('playerMoved', function (data) {
        self.movePlayer(data);
    });
    
    socket.on ('playerSendMsg', function (data) {
        self.displaySendMsg(data);
    });

    socket.on ('killPlayer', function (data) {
        self.removePlayer(data);
    });
    
};

Network.prototype.initializePlayers = function (data) {
    this.players = data.players;
    Network.id = data.id;

    for(var id in this.players){
        if(id != Network.id){
            this.players[id].entity = this.createPlayerEntity(this.players[id]);
        }
    }
    
    this.initialized = true;
    console.log('initialized');
};

Network.prototype.addPlayer = function (data) {
    this.players[data.id] = data;
    this.players[data.id].entity = this.createPlayerEntity(data);
};

Network.prototype.movePlayer = function (data) {
    if (this.initialized && !this.players[data.id].deleted) {
        this.players[data.id].entity.rigidbody.teleport(data.x, data.y, data.z, data.rx, data.ry, data.rz);
    }
};

Network.prototype.removePlayer = function (data) {
    if (this.players[data].entity) {
        this.players[data].entity.destroy ();
        this.players[data].deleted = true;
    }
};

Network.prototype.createPlayerEntity = function (data) {
    var newPlayer = this.other.clone();
    newPlayer.enabled = true;

    this.other.getParent().addChild(newPlayer);

    if (data)
    {
        newPlayer.rigidbody.teleport(data.x, data.y, data.z);
        newPlayer.findByName('otherNickText').element.text = data.nicknameVal;
    }

    return newPlayer;
};

// update code called every frame
Network.prototype.update = function(dt) {
    this.updatePosition();
};

Network.prototype.updatePosition = function () {
    if (this.initialized) {    
        var pos = this.player.getPosition();
        var rot = this.player.getEulerAngles();
        Network.socket.emit('positionUpdate', {id: Network.id, x: pos.x, y: pos.y, z: pos.z, rx: rot.x, ry: rot.y + 180, rz: rot.z});
    }
};





Network.prototype.sendMsg = function (nicknameVal, actualMsg) {
    this.displaySendMsg({id: Network.id, nicknameVal:nicknameVal, actualMsg:actualMsg});
    Network.socket.emit('sendMsgToAll', {id: Network.id, nicknameVal:nicknameVal, actualMsg:actualMsg});
    // console.log("sendMsg = ",{id: Network.id, nicknameVal:nicknameVal, actualMsg:actualMsg});
};

Network.prototype.displaySendMsg = function (data) {
    
    // console.log(data);
    if(data.nicknameVal !== "" && data.actualMsg !== "")
    {
        this.gamePlayManagerEntity.script.gamePlayManagerScript.addMsgInChatBox(data.nicknameVal,data.actualMsg);
    }
};



Can you fix it so the Body entity’s rotation is seen?

I need to see your old network.js script, to be able to see if there is any difference. Did you add some other scripts too that can cause the problem? I suggest to disable all recently added scripts.

1 Like

I get this error:
[network.js?id=67904620&branchId=7e5d4afd-15d3-49af-b083-0f872753849e:72]: Uncaught TypeError: Cannot read properties of null (reading ‘rigidbody’)

TypeError: Cannot read properties of null (reading ‘rigidbody’)
at scriptType.Network.movePlayer (https://launch.playcanvas.com/api/assets/files/scripts/connection/network.js?id=67904620&branchId=7e5d4afd-15d3-49af-b083-0f872753849e:72:38)
at r. (https://launch.playcanvas.com/api/assets/files/scripts/connection/network.js?id=67904620&branchId=7e5d4afd-15d3-49af-b083-0f872753849e:38:14)
at r.emit (https://launch.playcanvas.com/api/assets/files/scripts/connection/socket.js?id=67904596&branchId=7e5d4afd-15d3-49af-b083-0f872753849e:3:12375)
at r.onevent (https://launch.playcanvas.com/api/assets/files/scripts/connection/socket.js?id=67904596&branchId=7e5d4afd-15d3-49af-b083-0f872753849e:4:26293)
at r.onpacket (https://launch.playcanvas.com/api/assets/files/scripts/connection/socket.js?id=67904596&branchId=7e5d4afd-15d3-49af-b083-0f872753849e:4:25915)
at r. (https://launch.playcanvas.com/api/assets/files/scripts/connection/socket.js?id=67904596&branchId=7e5d4afd-15d3-49af-b083-0f872753849e:4:28033)
at r.emit (https://launch.playcanvas.com/api/assets/files/scripts/connection/socket.js?id=67904596&branchId=7e5d4afd-15d3-49af-b083-0f872753849e:3:12375)
at r.ondecoded (https://launch.playcanvas.com/api/assets/files/scripts/connection/socket.js?id=67904596&branchId=7e5d4afd-15d3-49af-b083-0f872753849e:3:18962)
at s. (https://launch.playcanvas.com/api/assets/files/scripts/connection/socket.js?id=67904596&branchId=7e5d4afd-15d3-49af-b083-0f872753849e:4:28033)
at s.r.emit (https://launch.playcanvas.com/api/assets/files/scripts/connection/socket.js?id=67904596&branchId=7e5d4afd-15d3-49af-b083-0f872753849e:3:12375)

I’ve already seen the error. It seems that there is no correct data. Can you please follow the steps I suggested?

Okay here is the network before:

var Network = pc.createScript('network');

Network.attributes.add('gamePlayManagerEntity', {type: 'entity'});


// static variables
Network.id = null;
Network.socket = null;

// initialize code called once per entity
Network.prototype.initialize = function() {
    // this.player = this.app.root.findByName('Player');
    // this.other = this.app.root.findByName('Other');
    
    this.player = this.gamePlayManagerEntity.script.gamePlayManagerScript.playerEntity;
    this.other = this.gamePlayManagerEntity.script.gamePlayManagerScript.otherEntity;
    
    // var socket = io.connect('https://playcanvas-multiplayer.glitch.me'); // Glitch hosted server
    var socket = io.connect('https://playcanvas-mp.glitch.me'); // Glitch hosted server
    Network.socket = socket;
    
    // socket.emit ('initialize');
    var nicknameVal = this.player.findByName('playerNickText').element.text;
    socket.emit ('initialize',{nicknameVal: nicknameVal});
    
    // this.displaySendMsg();
    
    var self = this;
    socket.on ('playerData', function (data) {
        self.initializePlayers (data);
    });

    socket.on ('playerJoined', function (data) {
        self.addPlayer(data);
    });

    socket.on ('playerMoved', function (data) {
        self.movePlayer(data);
    });
    
    socket.on ('playerSendMsg', function (data) {
        self.displaySendMsg(data);
    });

    socket.on ('killPlayer', function (data) {
        self.removePlayer(data);
    });
    
};

Network.prototype.initializePlayers = function (data) {
    this.players = data.players;
    Network.id = data.id;

    for(var id in this.players){
        if(id != Network.id){
            this.players[id].entity = this.createPlayerEntity(this.players[id]);
        }
    }
    
    this.initialized = true;
    console.log('initialized');
};

Network.prototype.addPlayer = function (data) {
    this.players[data.id] = data;
    this.players[data.id].entity = this.createPlayerEntity(data);
};

Network.prototype.movePlayer = function (data) {
    if (this.initialized && !this.players[data.id].deleted) {
        this.players[data.id].entity.rigidbody.teleport(data.x, data.y, data.z);
    }
};

Network.prototype.removePlayer = function (data) {
    if (this.players[data].entity) {
        this.players[data].entity.destroy ();
        this.players[data].deleted = true;
    }
};

Network.prototype.createPlayerEntity = function (data) {
    var newPlayer = this.other.clone();
    newPlayer.enabled = true;

    this.other.getParent().addChild(newPlayer);

    if (data)
    {
        newPlayer.rigidbody.teleport(data.x, data.y, data.z);
        newPlayer.findByName('otherNickText').element.text = data.nicknameVal;
    }

    return newPlayer;
};

// update code called every frame
Network.prototype.update = function(dt) {
    this.updatePosition();
};

Network.prototype.updatePosition = function () {
    if (this.initialized) {
        var pos = this.player.getPosition();
        Network.socket.emit('positionUpdate', {id: Network.id, x: pos.x, y: pos.y, z: pos.z});
    }
};




Network.prototype.sendMsg = function (nicknameVal, actualMsg) {
    this.displaySendMsg({id: Network.id, nicknameVal:nicknameVal, actualMsg:actualMsg});
    Network.socket.emit('sendMsgToAll', {id: Network.id, nicknameVal:nicknameVal, actualMsg:actualMsg});
    // console.log("sendMsg = ",{id: Network.id, nicknameVal:nicknameVal, actualMsg:actualMsg});
};

Network.prototype.displaySendMsg = function (data) {
    
    // console.log(data);
    if(data.nicknameVal !== "" && data.actualMsg !== "")
    {
        this.gamePlayManagerEntity.script.gamePlayManagerScript.addMsgInChatBox(data.nicknameVal,data.actualMsg);
    }
};



1 Like

If you replace the new code with the old code the error is gone? Otherwise the problem is caused by something else.

Okay I’ll try it.

1 Like

No error
https://launch.playcanvas.com/1345197?debug=true

2 Likes

But, what about fixing rotation? The player still cant see the other player’s body rotation.

1 Like

You get the error before the other player can move, so what do you expect? Error means it doesn’t work how we want.

When I have some time I go to compare my script with your script. I will let you know if I found something.

1 Like