Choppy movements strikes again

Hi guys,

I’m actually making a little unity game, just for fun and improving my idea managment. This game is suppose to be a 2-players , using the HLAPI of unity. I’m facing a problem for a week. I’ve made a really simple script for AI who just need to walk randomly (Using raycast mostly).

Here is the code for those who wants to check it out :

AI Script

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class AI : NetworkBehaviour {

    // General
    public int speed;
    public float vitesseAngle;

    [SyncVar]
    private CharacterController controller;

    [SyncVar]
    private Vector3 cDirection;
   
    // Rotations
    private float delayRotation;
    private float changeRotation;
    private float newAngle;

    // Anti-collisions
    private RaycastHit hit;
    public int safeDistance;

    // Marche & Pauses
    private int currentSpeed;
    private float delaySpeed;
    private float changeSpeed;

    void Start () {
        controller = GetComponent<CharacterController>();

        //gameObject.tag = "Bot";

        newAngle = Random.Range (0, 361);
        delayRotation = Random.Range (1, 6);
        delaySpeed = Random.Range (0, 6);

    }

    void Update () {

        float fixedTime = Global_V.fixedTime; 
        float deltaTime = Global_V.deltaTime;

       
        cDirection = Vector3.forward * currentSpeed;
        cDirection = transform.TransformDirection (cDirection);

        if (changeSpeed + delaySpeed < fixedTime)
        {
            changeSpeed = fixedTime;
            delaySpeed = Random.Range(0,10);
            if (currentSpeed == 0)
            {
                currentSpeed = speed;
            }
            else {
                currentSpeed = 0;
            }
        }

        if (changeRotation + delayRotation < fixedTime)
        {
            newAngle = Random.Range (0, 361);
            changeRotation = fixedTime;
            delayRotation = Random.Range (1, 6);
        }

        if (Physics.Raycast (transform.Find("Eye").position, transform.forward, out hit)) {
            if (hit.distance < safeDistance) {
                transform.rotation = Quaternion.Slerp (transform.rotation, transform.rotation * Quaternion.Euler (0, 180, 0), vitesseAngle * deltaTime * 2);
            } else {
                transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, newAngle, 0), vitesseAngle * deltaTime * 2);
            }
        }
        Debug.DrawRay(transform.Find ("Eye").position, transform.forward, Color.red,3);

        cDirection.y -= Global_V.gravity;
       
        controller.Move (cDirection * deltaTime);
    }

}

The problem is that my AI entities seem to work perfectly fine on the host client, but on user client, every AI looks like they’re lagging.

Here are two short GIF to let you know what it looks like.

Host client : https://gfycat.com/SafeHarshHoatzin

User client : https://gfycat.com/LateSorrowfulHylaeosaurus

After many try i can say that :

  • Number of AI doesn’t affect the lag.
  • I tried to sync RigidB, Character Controller and Transform, choppy movements are still here.
  • AI spawns OnStartServer().
  • I also tried to [SyncVar] the Vector3 of AI and its CharacterController, but nothing change.

Finally, here is the Inspector of the AI prefab :

AI INSPECTOR

If you guys can help me in anyway, i’d be very grateful.

(Forgive my bad english, it’s not my native language. :wink: )

NetworkTransform doesnt Interpolate. So It just teleports your player 9 times a second.
I recomend just writing a new one with Latency compensation or atleast with Position Lerp with either Interpolation or Extrapolation.

Well, its much more clear now. First I’m gonna try to use Position Lerp, and then, if it doesn’t work, latency compensation.

Thanks man.

Position lerp will work. But it will cause teleportation issues if a player has high and very fluctuating latency. Good luck.