This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
project-undercover/Project Undercover/Assets/Scripts/SimpleNPCBehavior.cs

74 lines
1.8 KiB
C#
Raw Normal View History

using UnityEngine;
using System.Collections;
2017-09-18 12:43:50 -05:00
using UnityEngine.AI;
public class SimpleNPCBehavior : Photon.PunBehaviour
{
2017-09-18 12:43:50 -05:00
private NavMeshAgent agent;
private bool setTarget = true;
enum State {
idle,
walking,
talking
};
void Start()
{
agent = GetComponent<NavMeshAgent>();
if (PhotonNetwork.isMasterClient) {
photonView.RPC("TeleportToTarget", PhotonTargets.All, GetRandomLocation());
photonView.RPC("SetColorRPC", PhotonTargets.All, new Vector3(Random.value, Random.value, Random.value));
}
}
void Update()
{
if (!PhotonNetwork.isMasterClient)
return;
if (setTarget)
{
setTarget = false;
StartCoroutine(UpdateDestination());
}
}
IEnumerator UpdateDestination()
{
yield return new WaitForSeconds(Random.Range(0.1f, 10.0f));
Vector3 location = GetRandomLocation();
photonView.RPC("SetTarget", PhotonTargets.All, location);
setTarget = true;
}
public static Vector3 GetRandomLocation()
{
2017-09-21 06:48:52 -05:00
var randTarget = new Vector3(5.0f - (10.0f * Random.value), 1.0f, 5.0f - (10.0f * Random.value));
NavMeshHit hit;
if (NavMesh.SamplePosition(randTarget, out hit, 1.0f, NavMesh.AllAreas))
return hit.position;
else
return GetRandomLocation();
}
[PunRPC]
void SetTarget(Vector3 target)
{
agent.destination = target;
}
[PunRPC]
void TeleportToTarget(Vector3 target)
{
2017-09-21 06:48:52 -05:00
agent.Warp(target);
agent.destination = target;
}
[PunRPC]
void SetColorRPC(Vector3 color)
{
transform.Find("Body").GetComponent<Renderer>().material.color = new Color(color.x, color.y, color.z);
}
}