I am doing 2d top down with 3D elements in unity. This is a multi-player game and the way I configure it is to have a main client (server) that transfers information to the server server through socket.io/ node.js.
I managed to synchronize the movement of the players, their health and rotation, as well as flipping through their sprites with great pleasure, given that this is the first time I am trying this feat.
However, now I decide to create monsters on this server, and I have a problem with which I did not have synchronization with the players. The way I did them was for the player controllers to transmit information to the server server, which is then passed on to other players. But with NPC, I simply connected the controller to the generated crowd, which performs actions on the "server" client, which is then transmitted to all clients so that everyone "agrees" on what happens and when.
It seems to work very well if there is only 1 monster, and the second I add 2 or more, they clearly contradict their information. Therefore, I see that I can not do the same with the players. And for this I need some feedback, if at all possible.
Signals are sent as follows:
command to create a monster for testing purposes:
void CreateMonster()
{
var selectedSpawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)];
var spawnedEnemy = Instantiate(Enemy[Random.Range(0, Enemy.Length)], selectedSpawnPoint.transform.position, selectedSpawnPoint.transform.rotation);
Debug.Log("im at " + spawnedEnemy.transform.position);
socket.Emit("enemySpawned", Network.VectorToJson(spawnedEnemy.transform.position));
}
the server receives this signal:
socket.on('enemySpawned', function(data){
var thisEnemyId = shortid.generate();
var currentTarget = '';
var Enemy = {
id: thisEnemyId, x:0, y:0, target:currentTarget,
};
Enemies[thisEnemyId] = Enemy;
Enemy.x = data.x;
Enemy.y = data.y;
data.id = thisEnemyId;
console.log("enemySpawned position: ", data);
socket.broadcast.emit('enemySpawned', data);
return to the server and transfer it to the creator:
public void SpawnEnemy(SocketIOEvent e)
{
var id = e.data["id"].str;
var spawnPosition = new Vector3(e.data["x"].n, e.data["y"].n, 0);
var enemy = Instantiate(orcShaman, spawnPosition, Quaternion.identity) as GameObject;
enemy.name = "Enemy " + id;
enemies.Add(id, enemy);
}
so far so good. everything is working fine and customers see the crowd in the right position at the right time.
The monster moves its position after hacking a player:
void Start () {
InvokeRepeating("CheckForMove", 1.0f, 0.1f);
}
void CheckForMove()
{
if (lastPos != transform.position)
{
socket.Emit("enemyMove", Network.VectorToJson(transform.position));
lastPos = transform.position;
}
}
The server receives information for moving:
socket.on('enemyMove', function(data){
Enemy.x = data.x;
Enemy.y = data.y;
data.id = thisEnemyId;
console.log('enemy is moving', data);
socket.broadcast.emit('enemyMove', data);
});
the game receives data:
private void OnEnemyMove(SocketIOEvent e)
{
var position = new Vector3(e.data["x"].n, e.data["y"].n, 0);
var enemy = enemySpawner.FindEnemy(e.data["id"].str);
var enemyNavigator = enemy.GetComponent<EnemyNavigator>();
enemyNavigator.MoveThisEnemy(position);
}
the individual monster should now receive information to move to the specified position:
public void MoveThisEnemy(Vector3 position)
{
rigid.MovePosition(position);
anim.SetBool("walk", true);
CheckFacingDirection();
}
, , , , , - . , . promt , 10 .
, , , , , -. , .
, , , , , , , apollo = (
.
. . , , . , , , - , , :
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var shortid = require('shortid');
app.use(express.static(__dirname));
var players = [];
io.on('connection', function(socket){
var thisPlayerId = shortid.generate();
var maxHealthValue = 0;
var currentHealthValue = 10;
var player = {
id: thisPlayerId, x:0, y:0, currentHealth:currentHealthValue, maxHealth: maxHealthValue, UpSideDown:0
};
players[thisPlayerId] = player;
console.log('client connected', thisPlayerId);
console.log('list of current players: ', players);
socket.emit('register', { id: thisPlayerId });
socket.broadcast.emit('otherPlayerConnected', player);
for(var everyoneButMe in players){
if(everyoneButMe == thisPlayerId)
continue;
console.log('not a main player joining');
socket.emit('otherPlayerConnected', players[everyoneButMe]);
socket.emit('register', { id: thisPlayerId });
socket.broadcast.emit('requestData');
}
for(var mainPlayer in players){
if(mainPlayer == thisPlayerId){
console.log('main player joining');
socket.emit('spawn', player);
}
};
var Enemies = [];
var currentTarget = '';
var Enemy = {
id: thisEnemyId, x:0, y:0, target:currentTarget,
};
var thisEnemyId = 0;
socket.on('enemySpawned', function(data){
thisEnemyId = Enemies.length ++
Enemies[thisEnemyId] = Enemy;
Enemy.x = data.x;
Enemy.y = data.y;
data.id = thisEnemyId;
console.log("enemySpawned position: ", data, thisEnemyId);
socket.broadcast.emit('enemySpawned', data);
});
socket.on('enemyMove', function(data){
Enemy.x = data.x;
Enemy.y = data.y;
data.id = thisEnemyId;
console.log('enemy is moving', data);
socket.broadcast.emit('enemyMove', data);
});
});
http.listen(process.env.PORT || 3000, function(){;
console.log('listening on *:3000');
});
console.log('-----------server started----------');