- Home /
Enemy's NavMesh not working
Hey guys.
I'm new in Unity and I don't know why my code doesn't work. When range is under 15, enemy should chase player, but instead of this it stops. Also, it is a little lagged when arrives at each point. How can I resolve this?
public Transform[] patrolPoints;
public float moveSpeed;
private int currentPoint;
private GameObject Player1;
private GameObject currentPlayer;
private bool Patrolling = true;
private NavMeshAgent navComp;
// Use this for initialization
void Start () {
currentPoint = 0;
Player1 = GameObject.FindGameObjectWithTag ("Player");
currentPlayer = Player1;
navComp = this.GetComponent<NavMeshAgent> ();
// transform.position = patrolPoints [0].position;
}
// Update is called once per frame
void Update () {
float range = Vector3.Distance (transform.position, currentPlayer.transform.position);
transform.position = Vector3.MoveTowards (transform.position, patrolPoints [currentPoint].position, moveSpeed * Time.deltaTime);
if (Patrolling) {
if (transform.position == patrolPoints [currentPoint].position) {
currentPoint++;
}
if (currentPoint >= patrolPoints.Length) {
currentPoint = 0;
}
if (range <= 15) {
Patrolling = false;
}
//transform.LookAt (patrolPoints [currentPoint].transform.position);
}
else if (Patrolling == false) {
//transform.position = Vector3.MoveTowards (transform.position, currentPlayer.transform.position, moveSpeed * Time.deltaTime);
navComp.SetDestination(currentPlayer.transform.position);
if (range >= 20) {
Patrolling = true;
}
}
}
}
Answer by vithorescames · Oct 17, 2016 at 12:10 PM
I figured out what was happening. Movetowards shouldn't be free in Update! Here is the code, working now!
public Transform[] patrolPoints;
public float moveSpeed;
private int currentPoint;
private GameObject Player;
private bool Patrolling = true;
private NavMeshAgent navComp;
// Use this for initialization
void Start () {
currentPoint = 0;
Player = GameObject.FindGameObjectWithTag ("Player");
navComp = this.GetComponent<NavMeshAgent> ();
// transform.position = patrolPoints [0].position;
}
// Update is called once per frame
void Update () {
float range = Vector3.Distance (transform.position, Player.transform.position);
if (range <= 20) {
Patrolling = false;
}
else if (range >= 20) {
Patrolling = true;
}
if (Patrolling) {
navComp.enabled = false;
transform.LookAt (patrolPoints [currentPoint].transform.position);
transform.position = Vector3.MoveTowards (transform.position, patrolPoints [currentPoint].position, moveSpeed * Time.deltaTime);
if (transform.position == patrolPoints [currentPoint].position) {
currentPoint++;
}
if (currentPoint >= patrolPoints.Length) {
currentPoint = 0;
}
}
else if (Patrolling == false) {
//transform.position = Vector3.MoveTowards (transform.position, currentPlayer.transform.position, moveSpeed * Time.deltaTime);
navComp.enabled = true;
navComp.SetDestination(Player.transform.position);
}
}
}
Awesome!!! post that as the answer so that people know :D
Thank you so much, you saved me after 9 hours of debugging! I really owe you
Answer by ZBlaxland · Oct 17, 2016 at 07:24 AM
I don't know if it helps, but here is a JavaScript that you may be able to use instead:
var theplayer : GameObject;
var speed : float;
var range : int;
var explosion : GameObject;
function Update () {
//RANGE - calculate the distance between the enemy and the player
range=Vector3.Distance(theplayer.transform.position,transform.position);
//AWARENESS – make the enemy look at the player when within 40 meters
if (range<40){
transform.LookAt(theplayer.transform.position);
}
//CHASE - move toward the player when within 15 and 30 meters
if (range<30&&range>15){
transform.Translate(Vector3.forward*speed);
}
//RETREAT – move away from the player when within 12 meters
if (range<12){
transform.Translate(-Vector3.forward*speed);
}
//DESTROY – Create an explosion and destroy the enemy when within 5 meters
if (range<5){
Instantiate(explosion,transform.position,transform.rotation);
Destroy(gameObject);
}
}
The values for speed and whatnot are easily changed, just remember to drag you character etc. into the given spaces on the script in the inspector before loading the game
Hope it works for you :D
Oh, I used a code similar to this, but I need the Nav$$anonymous$$esh to patchfinding, you know? I apreccitate your effort!