[SOLVED] Other player rotation not seen by player

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

thanks.

1 Like

Can you try to replace the line that gives the error with the same line that has the value 180 in it?

1 Like

I am not sure I know what you mean. Can you share an example?

1 Like

Sorry, I was looking wrong and I can’t find the issue right now. I need to debug this on my laptop when I have some time.

1 Like

I seriously think you are the best :smiley: :smiley: :smiley: :heart_eyes: :heart_eyes: :smiley: :smiley: :smiley:, and thanks again.

1 Like

I have debugged your project and contrary to what you said the error is also present with your own network.js script. That’s why I had to look further in your project.

I found an entity with a second network.js script in your hierarchy. This entity was disabled, but probably a script is still running on a disabled entity. Not sure if this is a bug. Maybe @yaustar can say more about this. To fix the error, disable also the script component or remove the entity if you don’t need it.

Furthermore, your current move-look-at.js script is not a good solution for your setup. It requires a rigidbody. To solve an error you have added a static rigidbody, but this cause other problems. I recommend using the script from my example project and remove the rigidbody from the child entity of the player.

Haven’t looked into but I’m guessing there’s callbacks on events. If there are and the intention is not to call them when the entity is disabled, then the code needs to unsubscribe from them when the entity is disabled and subscribe back when the entity is enabled

2 Likes

@yaustar found out that it’s not a bug, but you enable this entity by script. Because of that there where two network.js scripts running. It seems this result in an error on some devices.

1 Like

Can one of you guys just add the rotation to my project? Because I can’t everytime I try I break something.

I will see if I can achieve this with a fork of your project this weekend. I will post the result on the correct topic.

https://forum.playcanvas.com/t/entity-look-at-mouse-on-y-axis-in-2d/

1 Like

but still, the networking is wrong, and the player cannot see the other player’s rotation on the y axis, or at all.

1 Like

Sinds it works with the multiplayer tutorial project, it should also work with your project, as you use this project as base for your project I guess. The biggest difference I have seen is that you use for every player a camera as a child entity. I understand why you did this, but I’m not sure if that could be the reason it doesn’t work as expected.

1 Like

It is possible, although the player itself does not rotate, however, the body does, saying this I think you should add the Body entity to the network, with its position and rotation. And is it possible to do the same thing with a skin? Like enable a certain skin onclick of a button, and when the player starts the skin is sent through the network and all players see that player’s skin?

1 Like

Ah! That’s why it’s not working over the network right now. This will be fixed when I’m done with the forked project.

I think you already have a topic for this. Please keep every question in it’s own topic.