Nav Mesh Question
Is it possible to add a nav mesh to this enemy script so that it will avoid walls and not float into the air?
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(SphereCollider))]
public class EnemyAI : MonoBehaviour {
public float preceptionRadius = 10;
public Transform target;
public float movementSpeed = 5;
public float rotationSpeed = 420;
public bool dontMoveTorwardsPlayer;
public LayerMask MoveBlockers;
public Transform startCast, endCast;
private Transform myTransform;
// Use this for initialization
void Start ()
{
SphereCollider sc = GetComponent<SphereCollider>();
CharacterController cc = GetComponent<CharacterController>();
if (sc == null)
Debug.LogError("No Collider on enemy");
else
{
sc.isTrigger = true;
}
if(cc == null)
{
Debug.LogError("There's no Character Controller");
}
else
{
sc.center = cc.center;
sc.radius = preceptionRadius;
}
myTransform = transform;
}
void FixedUpdate()
{
Debug.DrawLine(startCast.position, endCast.position, Color.white);
dontMoveTorwardsPlayer = Physics.Linecast(startCast.position, endCast.position, MoveBlockers);
}
// Update is called once per frame
void Update()
{
if (!dontMoveTorwardsPlayer)
{
if (target)
{
Vector3 dir = (target.position - myTransform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
float dist = Vector3.Distance(target.position, myTransform.position);
//Find or Look at Target
myTransform.rotation = Quaternion.Lerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
//Movement
myTransform.position += myTransform.forward * movementSpeed * Time.deltaTime;
}
}
}
public void OnTriggerEnter(Collider other)
{
Debug.Log("Entered");
if (other.CompareTag("Player"))
{
target = other.transform;
}
}
public void OnTriggerExit(Collider other)
{
Debug.Log("Exit");
if(other.CompareTag("Player"))
{
target = null;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to get the closest object in the navMesh and making the agent move towards it 1 Answer
when enemy ai chase player enemy turns invisible 0 Answers
How Can I make a prefab using navmesh alternate destination targets? 0 Answers
Enemy AI for Multiplayer (using NavMeshAgent) unity 5.6.5f1 0 Answers
Line Renderer for golf game 2 Answers