hello everyone
i am facing an error in unity 3d and websockets .
problem :
1. the player is respawning every time after 2 seconds
2. Player animations are not updating in unity
this is my client side code …
const WebSocket = require('ws')
// create new websocket server
const wss = new WebSocket.Server({port: 8000})
// empty object to store all players
var players = {}
// add general WebSocket error handler
wss.on('error', function error (error) {
console.error('WebSocket error', error)
})
// on new client connect
wss.on('connection', function connection (client) {
console.log('new client connected')
// on new message recieved
client.on('message', function incoming (data) {
// get data from string
var [udid, x, y, z] = data.toString().split('\t')
// store data to players object
players[udid] = {
position: {
x: parseFloat(x),
y: parseFloat(y) + 1,
z: parseFloat(z)
},
timestamp: Date.now()
}
// save player udid to the client
client.udid = udid
})
})
function broadcastUpdate () {
broadcast messages to all clients
wss.clients.forEach(function each (client) {
// filter disconnected clients
if (client.readyState !== WebSocket.OPEN) return
// filter out current player by client.udid
var otherPlayers = Object.keys(players).filter(udid => udid !== client.udid)
// create array from the rest
var otherPlayersPositions = otherPlayers.map(udid => players[udid])
// send it
client.send(JSON.stringify({players: otherPlayersPositions}))
})
}
// call broadcastUpdate every 0.1s
setInterval(broadcastUpdate, 20000)
and this is my server side code
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
// define classed needed to deserialize recieved data
[Serializable]
public class Position {
public Vector3 position;
public int timestamp;
}
[Serializable]
public class Players {
public List<Position> players;
}
public class Multiplayer : MonoBehaviour {
// define public game object used to visualize other players
public GameObject otherPlayerObject;
private Vector3 prevPosition;
private List<GameObject> otherPlayers = new List<GameObject>();
IEnumerator Start () {
// get player
GameObject player = GameObject.Find("FPSController");
// connect to server
WebSocket w = new WebSocket(new Uri("ws://localhost:8000"));
yield return StartCoroutine(w.Connect());
Debug.Log("CONNECTED TO WEBSOCKETS");
// generate random ID to have idea for each client (feels unsecure)
System.Guid myGUID = System.Guid.NewGuid();
// wait for messages
while (true) {
// read message
string message = w.RecvString();
// check if message is not empty
if (message != null) {
// Debug.Log("RECEIVED FROM WEBSOCKETS: " + reply);
// deserialize recieved data
Players data = JsonUtility.FromJson<Players>(message);
// if number of players is not enough, create new ones
if (data.players.Count > otherPlayers.Count) {
for (int i = 0; i < data.players.Count - otherPlayers.Count; i++) {
otherPlayers.Add(Instantiate(otherPlayerObject, data.players[otherPlayers.Count + i].position, Quaternion.identity));
}
}
// update players positions
for (int i = 0; i < otherPlayers.Count; i++) {
// using animation
otherPlayers[i].transform.position = Vector3.Lerp(otherPlayers[i].transform.position, data.players[i].position, Time.deltaTime * 10F);
// or without animation
// otherPlayers[i].transform.position = data.players[i].position;
}
}
// if connection error, break the loop
if (w.error != null) {
Debug.LogError("Error: " + w.error);
break;
}
// check if player moved
if (prevPosition != player.transform.position) {
// send update if position had changed
w.SendString(myGUID + "\t" + player.transform.position.x + "\t" + player.transform.position.y + "\t" + player.transform.position.z);
prevPosition = player.transform.position;
}
yield return 0;
}
// if error, close connection
w.Close();
}
}
please help me with this issue …