Skip to content

Instantly share code, notes, and snippets.

@fallenblood7080
Created January 19, 2022 15:44
Show Gist options
  • Save fallenblood7080/595d8e3d2060e58786c3e1d3c79c6e42 to your computer and use it in GitHub Desktop.
Save fallenblood7080/595d8e3d2060e58786c3e1d3c79c6e42 to your computer and use it in GitHub Desktop.
//script of FOV Tutorial(Unity) - https://youtu.be/ztdj0KUnGuY
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fov_script : MonoBehaviour
{
public float fovAngle = 90f;
public Transform fovPoint;
public float range = 8;
public Transform target;
void Update()
{
Vector2 dir = target.position - transform.position;
float angle = Vector3.Angle(dir, fovPoint.up);
RaycastHit2D r = Physics2D.Raycast(fovPoint.position, dir, range);
if (angle < fovAngle / 2)
{
if (r.collider.CompareTag("Player"))
{
// WE SPOTTED THE PLAYER!
print("SEEN!");
Debug.DrawRay(fovPoint.position, dir, Color.red);
}
else
{
print("we dont seen");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment