Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Enemy AI following player help

$
0
0
I created a basic AI that follows the player around the map, however since my map is basically a maze with rooms added in, what can I do to prevent it from being stuck in a room, trying to get to my position, even though the player is across the entire map? This is the script, basically saying that when I look at the enemy, it freezes, and when I'm not, it follows my position. The script in its basics work fine, I just need to improve it. public class EntityBehavior : MonoBehaviour { public GameObject Enemy; public GameObject Player; public float Speed; RaycastHit hit; // Update is called once per frame void Update() { Vector3 forward = transform.TransformDirection(Vector3.forward) * 30; Debug.DrawRay(transform.position, forward, Color.green); Ray ray = new Ray(transform.position, transform.forward); if(Physics.Raycast(ray, out hit)) { Enemy.transform.position = Vector3.MoveTowards(Enemy.transform.position, Player.transform.position, Speed * Time.deltaTime); if(hit.collider.isTrigger) { Speed = 0f; } else { Speed = 1f; } } } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>