Hello, I am in the early stages of developing a 2D game, using the URP, and I have created a system where the enemy uses multiple ray casts and angles to create a FOV where the player can be detected. I would like to be able to draw these, but I have no documentation of how to do this online, so I am looking for help. I will provide the code:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class Enemy : MonoBehaviour
{
public PlayerController playerScript;
public float speed = 2f;
public float pauseTime = 2f;
public Transform groundDetection;
public LayerMask whatIsGround;
public LayerMask playerLayer;
public GameObject projectile;
public float projectileSpeed = 5f;
public float detectionAngle = 45f;
public int rayCount = 5;
public float raycastDistance = 2f;
public float shootCooldown = 2f;
public Vector2 spawnPos;
public Slider healthBar;
public float enemyDamage;
public float MaxHealth = 10;
public float Health = 10;
private bool movingRight = true;
private bool isPaused = false;
private GameObject player;
private float lastShootTime;
private Rigidbody2D rb;
void Start()
{
spawnPos = gameObject.transform.position;
healthBar.maxValue = MaxHealth;
healthBar.minValue = 0;
Health = MaxHealth;
player = GameObject.FindGameObjectWithTag("Player");
playerScript = player.GetComponent<PlayerController>();
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Health <= 0)
{
gameObject.transform.name = "Enemy(Killed)";
gameObject.transform.position = new Vector2(300, 0);
}
else
{
gameObject.transform.name = "Enemy(Alive)" + "(" + Health + ")";
}
if (Health >= MaxHealth)
{
Health = MaxHealth;
}
healthBar.value = Health;
if (!isPaused && !PlayerDetected())
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, 2f, whatIsGround);
if (groundInfo.collider == false)
{
if (!isPaused)
{
StartCoroutine(PauseAndTurn());
}
}
}
IEnumerator PauseAndTurn()
{
isPaused = true;
yield return new WaitForSeconds(pauseTime);
if (movingRight)
{
transform.eulerAngles = new Vector3(0, -180, 0);
healthBar.transform.localScale = new Vector3(-1, 1, 1);
movingRight = false;
}
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
healthBar.transform.localScale = new Vector3(1, 1, 1);
movingRight = true;
}
isPaused = false;
}
bool PlayerDetected()
{
float rayAngleStep = detectionAngle / (rayCount - 1);
Vector2 direction = player.transform.position - transform.position;
for (int i = 0; i < rayCount; i++)
{
float rayAngle = -detectionAngle / 2 + rayAngleStep * i;
Vector2 rayDirection = RotateVector(direction, rayAngle);
RaycastHit2D playerInfo = Physics2D.Raycast(transform.position, rayDirection, raycastDistance, playerLayer);
if (playerInfo.collider == true)
{
if (Time.time > lastShootTime + shootCooldown)
{
ShootPlayer();
lastShootTime = Time.time;
}
return true;
}
}
return false;
}
void ShootPlayer()
{
GameObject proj = Instantiate(projectile, transform.position, Quaternion.identity);
Rigidbody2D rb = proj.GetComponent<Rigidbody2D>();
Vector2 direction = (player.transform.position - transform.position).normalized;
rb.velocity = direction * projectileSpeed;
}
Vector2 RotateVector(Vector2 v, float degrees)
{
float radians = degrees * Mathf.Deg2Rad;
float sin = Mathf.Sin(radians);
float cos = Mathf.Cos(radians);
float tx = v.x;
float ty = v.y;
return new Vector2(cos * tx - sin * ty, sin * tx + cos * ty);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.CompareTag("Bullet"))
{
Health -= playerScript.damage;
}
}
}