|
I am making a AI script. I have already made it so the script checks if the enemy can see the player, based on a raycast that checks if there are any obsticals obscuring the enemy's view of the player.
The problem is that this gives the enemy a 360 degree view, how can i restrict it so the enemy can only see the player if the player is within a 135 degree cone in front of the enemy?
(comments are locked)
|
|
I found a solution to the problem, i used Vector3.Angle to detect how far away from the front of the enemey the player is. The code ended upp looking like this for anyone that migth be interested:
(comments are locked)
|
|
Thanks Mattivc!! This helped me a lot... I tried to figure out what some of your variables are since not all of them were declared in your code snippet. Here's my slightly modified version if anyone wants to check out. Just attach this javascript script to the enemy, drag your player to the playerObject variable, and be sure the player is tagged "Player"
(comments are locked)
|
|
Hi. I'm trying to implement this code but I can't get it to work for some reason. I'm learning 3d modelling and have no experience of coding, so I'm really in the dark as to what to do. I'm trying to get the enemy to look at the character if the code conditions are true, and have added the code I've tagged the player object as Pill, dragged the Player object onto the target parameter in the script option so it knows what to look at, and dragged the script onto the enemy sentry. Please help, as I'm pulling my hair out and I'm sure it's something blindingly simple. If you could tell me exactly what to do it'b be brilliant - I'm still just finding my way around Unity at the moment;
var fieldOfViewRange : float; // in degrees (I use 68, this gives the enemy a vision of 136 degrees) var minPlayerDetectDistance : float; // the distance the player can come behind the enemy without being deteacted var rayRange : float; // distance the enemy can "see" in front of him private var rayDirection = Vector3.zero; var target : Transform; function CanSeePlayer() : boolean { var hit : RaycastHit; rayDirection = playerObject.transform.position - transform.position; var distanceToPlayer = Vector3.Distance(transform.position, playerObject.transform.position);
}
{ // Draws a line in front of the player and one behind this is used to visually illustrate the detection ranges in front and behind the enemy Gizmos.color = Color.magenta; // the color used to detect the player in front Gizmos.DrawRay (transform.position, transform.forward * rayRange); Gizmos.color = Color.yellow; // the color used to detect the player from behind Gizmos.DrawRay (transform.position, transform.forward * -minPlayerDetectDistance);
(comments are locked)
|
|
Thanks both of you, this is usefull as there are not much code for this. I'm going to try it.
(comments are locked)
|
Unity Answers has moved to a new system, and some users may have trouble logging in.