I’ve been working on my game, and I decided that it is about that time to start working on the multiplayer aspect of it. I’m completely new to this networking thing, and I would like to use the built in “Unet” for the game.
I read the documentation for a lot of the networking things, and I’ve been trying to follow along on some tutorials, but no matter what I do, I can’t stop this rubber banding issue. Here is the code I’ve written.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;
[NetworkSettings(channel = 0, sendInterval = 0.1f)]
public class SyncPlayers : NetworkBehaviour {
[SyncVar (hook = "UpdateHistoric")] Vector3 syncPos;
[SerializeField] Transform myPlayer;
private float lerpSpeed;
private Vector3 lastPos;
private float moveInterval;
private float historicRemovalRange;
private List<Vector3> historicPositions = new List<Vector3>();
void Start () {
lerpSpeed = 18.0f;
moveInterval = 0.1f;
historicRemovalRange = 0.05f;
//rotateInterval = 5;
}
void Update () {
SendPosition ();
lerpPosition ();
}
void lerpPosition() {
if (!isLocalPlayer) {
if(historicPositions.Count > 0) {
myPlayer.position = Vector3.Lerp(myPlayer.position, historicPositions[0], lerpSpeed * Time.deltaTime);
if(Vector3.Distance(myPlayer.position, historicPositions[0]) < historicRemovalRange) {
historicPositions.RemoveAt (0);
}
}
}
}
[Command]
void CmdSendPosition(Vector3 pos) {
syncPos = pos;
}
[ClientCallback]
void UpdateHistoric(Vector3 positionHistory) {
historicPositions.Add (positionHistory);
}
[ClientCallback]
void SendPosition() {
if (isLocalPlayer && Vector3.Distance(myPlayer.position, lastPos) > moveInterval) {
CmdSendPosition (myPlayer.position);
lastPos = myPlayer.position;
}
}
}
Keep in mind I have turned on network simulator to 100 milliseconds of delay time to simulate lag.
So basically what I am trying to do is store a history of every players movements, and then lerp all other non-local players to those positions to compensate for any lag the user might have. From my understanding, this script should store a bunch of “Waypoints” into a list whenever the server changes another players position, and that player object should lerp towards each waypoint in order.
Now 2 things are happening.
#1, The player seems to increase/decrease in speed as it approaches each waypoint. This looks very ugly, and I have a feeling it has to do with the way I’m lerping, but I have found no other alternatives.
#2 Occasionally, the player will lerp backwards, and then forwards for no apparent reason. This is the rubber banding problem that I have.
Any help would be greatly appreciated, this has given me a headache since yesterday.
Also, if anyone happens to know of where to find a good tutorial for Unet, please fill me in. I would love to learn how to properly write Unet code, and I haven’t been able to find any decent tutorials for beginners.