Hi guys! For my grid-based game I’d like to create a field of view for my enemies that is visible to the player. The enemies can only look in two directions (up and down or left and right). Also they won’t change the direction they are looking during the game.
I have tried to make the field visible with the help of the raycast I have already set up to detect whether or not the player is in their line of sight but I couldn’t make it work. I have watched many tutorials and threads discussing this but I only found field of view visualisations that originate from one point and form a cone. What I need is a rectangular field of view that changes size with the help of the raycast (so it won’t show through obstacles).
If anyone has suggestions on how to create this effect I would be very thankful!
Here is my Enemy Sight Script in case it helps:
PathFollower pathFollower;
public GameObject sightline;
public GameObject player;
float rayLength = 20f;
bool onSameZAxis = false;
Ray myRay;
RaycastHit hit;
void Start()
{
GameObject g = GameObject.Find("Path");
pathFollower = g.GetComponent<PathFollower>();
}
void Update()
{
DrawRay();
if (player.transform.position.z.Equals(transform.position.z) && pathFollower.hasCheckedAxis == false)
{
onSameZAxis = true;
}
else { onSameZAxis = false; }
//checks only when enemy stands still if the player is in sight and on same axis
if (onSameZAxis == true && pathFollower.standsStill == true && PlayerInSight() == true)
{
Debug.Log("spotted");
pathFollower.sawPlayer = true;
pathFollower.hasCheckedAxis = true;
//onSameZAxis = false;
//Debug.Log("In the If-Loop");
}
}
//checks if there is a wall between the player and the enemy
bool PlayerInSight()
{
myRay = new Ray(transform.position + new Vector3(0, 0.15f, 0), -transform.right);
Debug.DrawRay(myRay.origin, myRay.direction, Color.red);
if (Physics.Raycast(myRay, out hit, rayLength))
{
if (hit.collider.tag == "Player")
{
return true;
}
}
return false;
}
void DrawRay()
{
//Moves the Sightline Object.
}
}



