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
2017-09-27 23:56:52 -05:00

80 lines
No EOL
1.9 KiB
C#

using UnityEngine;
using System.Collections;
using UnityEngine.AI;
public class SimpleNPCBehavior : Photon.PunBehaviour
{
private bool setTarget = true;
enum State {
idle,
walking,
talking
};
void Start()
{
//agent = gameObject.AddComponent<NavMeshAgent>();
if (PhotonNetwork.isMasterClient) {
photonView.RPC("TeleportToTarget", PhotonTargets.All, GetRandomLocation());
photonView.RPC("SetColorRPC", PhotonTargets.All, new Vector3(Random.value, Random.value, Random.value));
Debug.Log("setting color");
}
}
void Update()
{
if (!PhotonNetwork.isMasterClient)
return;
if (setTarget)
{
setTarget = false;
StartCoroutine(UpdateDestination());
}
}
NavMeshAgent GetAgent()
{
return GetComponent<NavMeshAgent>();
}
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()
{
var randTarget = new Vector3(5.0f - (10.0f * Random.value), 0.0f, 5.0f - (10.0f * Random.value));
NavMeshHit hit;
if (NavMesh.SamplePosition(randTarget, out hit, 1.0f, NavMesh.AllAreas))
//return hit.position;
return new Vector3 (hit.position.x, 1.0f, hit.position.z);
else
return GetRandomLocation();
}
[PunRPC]
void SetTarget(Vector3 target)
{
GetAgent().destination = target;
}
[PunRPC]
void TeleportToTarget(Vector3 target)
{
GetAgent().Warp(target);
GetAgent().destination = target;
}
[PunRPC]
void SetColorRPC(Vector3 color)
{
transform.Find("Body").GetComponent<Renderer>().material.color = new Color(color.x, color.y, color.z);
}
}