- Home /
Question by
black_addict2 · May 19, 2021 at 11:39 AM ·
c#multiplayernpcfollow playerchase
NPC does not follow player movement
I'm making a multiplayer game, so the NPC will following the player when at a certain distance. Players are prefabs that cloned because its multiplayer, the NPC just follow the static position of the prefab (x y z=0) even the player is moving. Player prefab does not exist in the hierarchy its called when the game start, I don't know if it has any effect.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class npcAI : MonoBehaviour
{
Animator anim;
public GameObject player;
public GameObject GetPlayer()
{
return player;
}
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
anim.SetFloat("distance", Vector3.Distance(transform.position, player.transform.position));
}
}
In the "player" reference I set the player prefab, what should I do to fix it
Comment
What do you mean by static position? Generally making something static makes it shared amongst instances, which is not what you want. Also you can't assign the prefab as the player reference you need a reference to the player in the scene. When you instantiate a new player is a good time to setup that reference.