Hello everyone
I’m kinda having a trouble with making an enemy stopping at certain distance with this script the game is 2D game
and here’s the script I have for enemy (following) purpose
using UnityEngine;
using System.Collections;
public class FacePlayer : MonoBehaviour {
public float rotSpeed = 180f;
public Transform player;
void Update () {
if (player == null)
{
GameObject go = GameObject.Find ("Player");
if (go != null){
player = go.transform;
}
}
if (player == null)
return;
Vector3 dir = player.position - transform.position;
dir.Normalize ();
float zAngle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg - 90;
Quaternion desiredRot = Quaternion.Euler (0, 0, zAngle);
transform.rotation = Quaternion.RotateTowards (transform.rotation, desiredRot, rotSpeed * Time.deltaTime);
}
}
So what am I missing? I tried in update function tried this
if (Vector3.Distance (transform.position, Player.position) > maxDistance)
and I added the maxDistance value but the enemy starts moving forward and keeps going forward for some reason.
Thanks in advance!