I’ve seen similar questions but none of the answers have worked for me
So I have a top-down 2d shooter game
Currently, my Enemies are shooting at the player right from the start. However, these players are behind cover. So I would want them only to shoot when there is no cover between them and the player. How can I implement this? I’ve seen some ideas for making a 2d Raycast but those didn’t work for me as they just stopped the enemies from shooting in general
Here is a screenshot of my Unity window and the EnemyShooting code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShooting : MonoBehaviour
{
public GameObject EnemyBullet;
public GameObject player;
public Transform bulletPos;
public float bulletSpeed = 2f;
public float shootingRange = 10f;
private float timer;
void Update()
{
timer += Time.deltaTime;
if (timer > 0.5 && Player.Alive)
{
timer = 0;
EnemyShoot();
}
}
void EnemyShoot()
{
GameObject bullet = Instantiate(EnemyBullet, bulletPos.position, bulletPos.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(bulletPos.up * bulletSpeed, ForceMode2D.Impulse);
}
}
In this picture, the border of the roads has colliders therefore the enemies behind them shoot at me.
