Npc not syncing correctly on all clients

Hello, I have made a npc and I'm trying to let it walk across the map. For some reason the npc is not starting at the same time on all connected clients and when reached it's end position the npc teleports for one client but not for the other one (shoudn't teleport). Does anyone know what I've done wrong or what I'm missing to get this to work ?

Video: https://youtu.be/fZrDemwvOdw
Npc photon info: https://prnt.sc/vwsvy1
Spawning the npc:
PhotonNetwork.Instantiate("ghost", new Vector3(0, -1, 0), new Quaternion(0, 0, 0, 0));

Npc movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Prankster : MonoBehaviour
{
    public AudioSource mainSource;
    public NavMeshAgent agent;

    private GameObject playerParent;
    private GameObject startPoints;
    private GameObject goToPoints;

    private bool inWall = false;
    private bool leaveView = false;
    private Vector3 wallStartPos;
    private Quaternion endRotation;
    private float distanceFromStartWall = 2f;

    private void Start()
    {
        playerParent = GameObject.Find("Players");
        startPoints = GameObject.Find("StartPoint");
        goToPoints = GameObject.Find("GoToPoints");

        StartCoroutine(AI());
    }

    private void FixedUpdate()
    {
        if(inWall)
        {
            if ((transform.position - wallStartPos).magnitude >= distanceFromStartWall)
            {
                inWall = false;
                mainSource.Play();

                agent.enabled = true;
                FindGoToPoint();
            }
            else transform.position = Vector3.MoveTowards(transform.position, (transform.position + transform.forward * 1), .5f * Time.deltaTime);
        }

        if (leaveView)
        {
            if ((transform.position - wallStartPos).magnitude >= distanceFromStartWall)
            {
                leaveView = false;
                StartCoroutine(AI());
            }
            else transform.position = Vector3.MoveTowards(transform.position, (transform.position + transform.forward * 1), .5f * Time.deltaTime);
        }

        if (!agent.enabled && agent.destination == null) return;
        if ((transform.position - agent.destination).magnitude < 1.1f)
        {
            mainSource.Stop();
            agent.enabled = false;

            wallStartPos = transform.position;
            transform.rotation = endRotation;

            leaveView = true;
        }
    }

    IEnumerator AI()
    {
        yield return new WaitForSeconds(Random.Range(10, 30));
        Spook();
    }

    private void FindGoToPoint()
    {
        GameObject point = null;

        foreach(Transform p in goToPoints.transform)
        {
            if(point == null)
            {
                if ((transform.position - p.position).magnitude >= 16)
                {
                    point = p.gameObject;
                }
            }else
            {
                if ((transform.position - p.position).magnitude < (transform.position - point.transform.position).magnitude)
                {
                    if ((transform.position - p.position).magnitude >= 16)
                    {
                        point = p.gameObject;
                    }
                }
            }
        }

        endRotation = point.transform.rotation;
        agent.destination = new Vector3(point.transform.position.x, transform.position.y, point.transform.position.z);
    }

    private GameObject FindStartPoint(Transform player)
    {
        GameObject point = null;

        foreach (Transform p in startPoints.transform)
        {
            if (point == null)
            {
                point = p.gameObject;
            }
            else
            {
                if ((player.position - p.position).magnitude < (player.position - point.transform.position).magnitude)
                {
                    point = p.gameObject;
                }
            }
        }

        return point;
    }

    private void Spook()
    {
        GameObject player = playerParent.transform.GetChild((Random.Range(1, playerParent.transform.childCount) - 1)).gameObject;
        GameObject startPoint = FindStartPoint(player.transform);

        wallStartPos = startPoint.transform.position;
        transform.position = new Vector3(startPoint.transform.position.x, 1, startPoint.transform.position.z);
        transform.rotation = startPoint.transform.rotation;

        inWall = true;
    }
}

Comments

  • Anyone know's anything that has a chance of fixing this?
  • Jens00
    Jens00
    edited December 2020
    I found the problem, added a checker to see if the current client was the masterClient and only made the script run when it was.
  • just a headsup, if you're creating an object that isn't going to be controlled by a player, use PhotonNetwork.InstantiateRoomObject.
    RoomObjects are different are don't have an Actor ID / are meant to be an object that isn't controlled by a player.