Hi everyone.
Right off the bat, I’ve already read several tutorials as well as Valve’s documentation on interpolation. It wasn’t terribly helpful, and all of the examples I’ve found on interpolation are over my head.
I’m after a multiplayer game over LAN (8 or less players). It will be LAN, and thus the ping will be very low. I’d like the movement of each player from each clients perspective to be smooth, as by default it’s a bit jittery.
My code is as follows. Two simple scripts. One global network management script, and a player movement script attached to the player object that is instantiated for each player that joins. Each player sees each others player movement as jittery. I’ve been unsuccessful at basic interpolation.
Because I’m doing this for LAN, I’m assuming the interpolation can be much more basic and less complicated. I tried changing the sendRate from 15 to something higher, but that didn’t seem to make any difference at all. Even values such as 500 didn’t make a difference.
All of the various links to the networking examples published by Unity are dead. I can’t find any official interpolation examples.
The code I have so far is as follows.
NetworkManager.cs
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour
{
public GameObject playerPrefab;
void StartServer()
{
Network.InitializeServer(32, 25000, false);
Network.sendRate = 15;
Debug.Log("Server initialized, sendRate: " + Network.sendRate);
}
void ConnectToServer()
{
Network.Connect("127.0.0.1", 25000, "");
}
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 100, 20), "Start Server"))
StartServer();
if (GUI.Button(new Rect(200, 20, 100, 20), "Join Game"))
ConnectToServer();
}
void OnConnectedToServer()
{
Network.Instantiate(playerPrefab, new Vector3(0, 0.5f, 0), Quaternion.identity, 0);
}
void OnDisconnectedToServer()
{
Network.Destroy(playerPrefab);
}
}
PlayerMovement.cs
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
private float x = 0;
private float z = 0;
public Vector3 currentPos;
void Update ()
{
if (networkView.isMine)
{
currentPos = new Vector3(x, 0.5f, z);
if (Input.GetKey("left"))
x -= 1f * Time.deltaTime;
if (Input.GetKey("right"))
x += 1f * Time.deltaTime;
if (Input.GetKey("down"))
z -= 1f * Time.deltaTime;
if (Input.GetKey("up"))
z += 1f * Time.deltaTime;
transform.position = currentPos;
}
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Vector3 pos = Vector3.zero;
if (stream.isWriting)
{
pos = Vector3.Lerp(pos, currentPos, Time.time);
stream.Serialize(ref pos);
}
else
{
stream.Serialize(ref pos);
currentPos = Vector3.Lerp(pos, currentPos, Time.time);
}
}
}