I haven’t made the script, but before I start working on it I would like some conformation it will work. Raycasting seems ok, but to be honest, for a Field of Vision, I think making a ton of invisible box collider triggers, shape them so they look life a field of vision, the more you go out the smaller and less height it has. Then create a script that if the player hits the box collider, they play an animation, like alert, or something. Would this work?
thats what I was planning to do, except with boxes. Cones do seem like a better idea though. Are there any other ways of doing it aside from raycasting?
i know u said no raycasts but something like this wouldn’t work?
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
private bool pVisable = false; //Is the player visable?
private bool facingPlayer = false; //is the enemy facing the player?
private RaycastHit playerVisable; //Works with RayCast for more info read Unity API
public Transform player; //Since you only will have one player this should work just fine
public float distance = 3; //Distance the from the Player until the enemy can see the player
void Awake(){
//Makes sure the player Transform variable isn't empty only works if the player is named Player
if(player == null){
GameObject p = GameObject.Find("Player");
player = p.transform;
}
}
void Update(){
//Makes sure the player is facing forward
Vector3 enemyFront = transform.TransformDirection (Vector3.forward);
//Checks to see if the enemy can see and is close enough to interact with the player
if (Physics.Raycast(transform.position, enemyFront, out playerVisable, distance))
{
if (playerVisable.transform == player){
pVisable = true;
facingPlayer = true;
Debug.Log("Found the player! He is 3 Paces away");
} else {
pVisable = false;
facingPlayer = false;
Debug.Log("there is something obstructing the view...");
}
}
}
}
I would use Linear Algebra to see if doing a raycast or any other collision is even worth it.
The algebra test will tell you
distance to target
if in field of view
This is similar to doing the collider test with a cone but can be optimized and falls outside of unity’s physics.
You could do all of that using unity physics with colliders though (sphere for distance or awareness activation, cone for visibility). However i still think you would need a raycast to see if the player is behind a wall or something.
@KingCharizard I don’t think that would work unless the enemy is facing directly at the player which is a very rare case. We could simulate that, however, by making the enemy face the player direction when awareness raises.
I think making tons and tons of colliders to do a vision test is a terrible idea btw.