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

Enemy line of sight

$
0
0
I'm having trouble with raycasting! Basically, my enemy has a sphere collider I'm using as its maximum sight distance, and when the player enters this, it tells the enemy to chase the player. That part works fine, but the problem is that the enemy can "see" the player through walls because the collider goes through walls. I'm not fussed about field of view since these enemies are supposed to have a 360 degree line of sight. I'm trying to use raycasts so the enemy will ignore the player if there is a wall in between them, but nothing I'm trying has worked so far. Here's my enemy sight code, shamelessly looted from the stealth tutorial. { private GameObject playerTarget; private SphereCollider col; void Awake () { playerTarget = GameObject.FindGameObjectWithTag ("Player"); col = GetComponent(); } void OnTriggerEnter (Collider other) { if(other.gameObject == playerTarget) { Vector3 direction = playerTarget.transform.position - transform.position; { RaycastHit hit; if(Physics.Raycast(transform.position + transform.up, direction.normalized, out hit, col.radius)) { if(hit.collider.gameObject == playerTarget) { Debug.Log ("player in sight"); //Stuff happens that makes the enemy chase the player } } } } } } This code doesn't work 100% of the time. Sometimes I get the debug "player in sight" sometimes I don't, if the player goes into the collider when a wall is between them, nothing happens, but re-entering the collider with nothing in the way still has nothing happen. It's like the raycast happens only once, regardless of how many times the player enters and exits the collider sphere. Trouble is the tutorial never really explains exactly what a raycast is and how they work. Are they linear beams or do they fill the entire sphere?

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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