Last active
February 28, 2020 18:48
-
-
Save Templar2020/db7b5a3c1fcf03b1264646513022b846 to your computer and use it in GitHub Desktop.
Improved Wolf AI with navmesh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class NavWolfAI : MonoBehaviour { | |
// Public | |
public Transform player; | |
public float speed; | |
public int damage; | |
// Wander | |
public float wanderRadius; | |
public float wanderTimer; | |
//Detection | |
public float alertDist; | |
public float attackDist; | |
// Private | |
private Animator state; | |
private Vector3 direction; | |
private Transform target; | |
private UnityEngine.NavMeshAgent agent; | |
private float timer; | |
private float distance; | |
void OnEnable () { | |
agent = GetComponent<UnityEngine.NavMeshAgent> (); | |
timer = wanderTimer; | |
} | |
// Use this for initialization | |
void Start () { | |
state = GetComponent<Animator>(); | |
distance = Vector3.Distance(player.position, transform.position); | |
} | |
// Update is called once per frame | |
void Update () { | |
distance = Vector3.Distance(player.position, transform.position); | |
// Alert | |
if(distance < alertDist && distance > attackDist){ | |
print("Wolf sees player"); | |
state.SetBool("isFollowing",true); | |
state.SetBool("isWandering",false); | |
state.SetBool("isAttacking",false); | |
speed = speed + 2; | |
transform.LookAt(player); | |
transform.Translate(Vector3.forward*speed*Time.deltaTime); | |
} | |
//Attacking | |
else if(distance <= alertDist){ | |
print("Wolf is following!"); | |
direction = player.position - transform.position; | |
direction.y = 0; | |
//transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction),0.09f*Time.deltaTime); | |
//transform.Translate(Vector3.forward*speed*Time.deltaTime); | |
//transform.Translate(0,0,speed*Time.deltaTime); | |
state.SetBool("isFollowing",false); | |
state.SetBool("isAttacking",true); | |
state.SetBool("isWandering",false); | |
speed = speed - 10; | |
transform.LookAt(player); | |
transform.Translate(Vector3.forward*speed*Time.deltaTime); | |
if(direction.magnitude <= attackDist){ | |
print("wolf is attacking!"); | |
state.SetBool("isFollowing",false); | |
state.SetBool("isAttacking",true); | |
state.SetBool("isWandering",false); | |
// var hit = player.gameObject; | |
// var health = hit.GetComponent<playerHealth>(); | |
// if(health != null){ | |
// health.TakeDamage(damage); | |
// } | |
} | |
} | |
//Wandering | |
else if(distance > alertDist){ | |
timer += Time.deltaTime; | |
state.SetBool("isFollowing",false); | |
state.SetBool("isAttacking",false); | |
state.SetBool("isWandering",true); | |
if (timer >= wanderTimer) { | |
Vector3 newPos = RandomNavSphere(transform.position, wanderRadius, -1); | |
agent.SetDestination(newPos); | |
timer = 0; | |
} | |
} | |
} | |
public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask) { | |
Vector3 randDirection = Random.insideUnitSphere * dist; | |
randDirection += origin; | |
UnityEngine.NavMeshHit navHit; | |
UnityEngine.NavMesh.SamplePosition (randDirection, out navHit, dist, layermask); | |
return navHit.position; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using UnityEngine;
using System.Collections;
public class NavWander : MonoBehaviour {
}