Skip to content

Instantly share code, notes, and snippets.

@gino0717
Last active September 9, 2015 04:17
Show Gist options
  • Select an option

  • Save gino0717/43f57945369fcf71ecc5 to your computer and use it in GitHub Desktop.

Select an option

Save gino0717/43f57945369fcf71ecc5 to your computer and use it in GitHub Desktop.
the Navigation code for NPCs catch up the player
using UnityEngine;
using System.Collections;
namespace UnityStandardAssets.Characters.ThirdPerson
{
public class nav : MonoBehaviour {
public Transform aa;
public NavMeshAgent Nav{ get; private set; }
public int attackRange= 100;
public float attackangle=90.0f;
public Transform target;
bool die;
public Collider player;
RaycastHit hit;
Animator anim;
public GUISkin skin;
public ThirdPersonCharacter character { get; private set; }
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
Nav = GetComponentInChildren<NavMeshAgent>();
character = GetComponent<ThirdPersonCharacter>();
}
// Update is called once per frame
void Update () {
print (CanSeeTarget ());
if (CanSeeTarget ()) {
Nav.destination = aa.position;
character.Move (Nav.desiredVelocity, false, false);
}
if(!CanSeeTarget())
{
character.Move(Vector3.zero, false, false);
}
}
bool CanSeeTarget (){
if (Vector3.Distance (transform.position, target.position) > attackRange)
return false;
Vector3 targetdir = target.position - transform.position;
if (Physics.Linecast (transform.position, target.position, out hit)) {
if ((Vector3.Angle (transform.forward, targetdir) < attackangle))
{
return hit.transform == target;
}
}
return false;
}
void OnGUI()
{
if (CanSeeTarget ()) {
GUI.Box (new Rect (Screen.width / 2, 10, 120, 60), "");
GUI.Label (new Rect (Screen.width / 2, 10, 120, 60), "你被鄉民發現惹!");
}
if (die == true) {
Application.LoadLevel(1);
}
}
void OnTriggerEnter(Collider other)
{
if (other == player)
die = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment