using UnityEngine;
using System.Collections;
public class EnemySight: MonoBehaviour
{
public GameObject player;
public float fovAngle;
public bool playerInSight;
void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject == player)
{
Vector2 playerDir = other.transform.position - transform.position;
float angle = Vector3.Angle(playerDir, transform.right);
if (angle < fovAngle / 2)
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, playerDir.normalized);
if (hit.collider.gameObject == player)
{
Debug.DrawRay(transform.position, playerDir.normalized, Color.green);
print("halp meh");
}
}
}
}
}
This is a script for an enemy view script for a 2d top down stealth game. The part that isn’t working is the last bit involving the raycasts. Everything works up to the hit.collider.gameObject == player
. Nothing prints and the debug ray is not drawn. If I change it to hit.collider != null
it “works”, but the raycasts hit the enemy itself. There are no actual errors, and the variables are all defined in-editor. Any help/comments are appreciated. Relatively new to Unity so it’s probably a dumb mistake.