Rectangular Field Of View

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.
    }
}

Hi,
do you mean you want to shoot rays in a row like this?
5165915--512537--upload_2019-11-12_12-33-16.png

or maybe your screenshot could help more.

Yes excactly! Here is a screenshot of how it should look like:

5165939--512546--Unity_BtCK4Uohwk.png

some terrible quick code, but does this (image below)

idea is to just shoot multiple rays inside for-loop, i take box width in script, vector3.lerp to shoot any amount of rays,
within that box size, but you could also set empty transforms there to mark start and end positions for rays…

5166215--512573--upload_2019-11-12_13-59-43.png

1 Like

This is great! Thank you so much! Is there a way to make this visible in Playmode too?

in editor you can enable gizmos 5166575--512633--upload_2019-11-12_15-52-11.png

in build, you can use GL Lines to draw Unity - Scripting API: GL.LINES

1 Like