How do I make exactly this field of view?

a clue please… :face_with_spiral_eyes: if possible in the form of code…

Raycast from enemy to player. If the angle between that ray and the enemy’s .forward vector is less than (FoV angle), then the player’s within the field of view.

I have tried with raycast… I want to do exactly like the picture… :face_with_spiral_eyes:

Do what, exactly? Loius’s suggestion is correct, with a couple corrections. A raycast is unnecessary. Simply subtract the player’s position from the NPC’s position to get the vector between them. Compute the angle between that vector and the NPC’s forward vector. If that angle is less-than-or-equal-to half of the NPC’s FOV angle, and if the length of that vector is less-than-or-equal-to the NPC’s maximum view distance, then the player is inside the yellow boundary.

bool IsPlayerVisible(Transform playerTransform, Transform npcTransform, float npcViewAngle, float npcViewDistance)
{
    Vector3 npcToPlayer = playerTransform.position - npcTransform.position;
    float angleToPlayer = Vector3.Angle(npcToPlayer, npcTransform.forward);
    float distanceToPlayer = npcToPlayer.magnitude;

    if (angleToPlayer <= npcViewAngle/2  distanceToPlayer <= npcViewDistance)
        return true;

    return false;
}

thanks friend but could you please write in the form of javascript? :roll_eyes: sorry…

I wanted exactly vision cone. please help… :frowning:

That’s exactly what the function I gave you does.

I will convert it to Unityscript for you, but programming and math are minimal skills that all game developers need. You should use this problem as practice, since it is very simple. :slight_smile:

This will work, assuming there are no bugs.

function IsPlayerVisible(playerTransform:Transform, npcTransform:Transform, npcViewAngle:float, npcViewDistance:float):boolean
{
    var npcToPlayer:Vector3 = playerTransform.position - npcTransform.position;
    var angleToPlayer:float = Vector3.Angle(npcToPlayer, npcTransform.forward);
    var distanceToPlayer:float = npcToPlayer.magnitude;
 
    if (angleToPlayer <= npcViewAngle/2  distanceToPlayer <= npcViewDistance)
        return true;

    return false;
}

very very thank you Briant!! problem is solved. :smile:

from İstanbul greetings Sammamish!! :wink:

how can i possibly draw this? i was made field of vision and it can detect collision or some tag too, but the vision area is imaginary, maybe you have any idea? (i tried draw gizmo but it always failed :frowning: )