How can i hide from enemy behind a wall?

Hi,
I am trying to make 2d top-down shooter game. I have an enemy ai script. Enemy can make patrol and if my player inside enemy distance can follow my player. My question is how can i hide behind a wall? I tryin to make enemy can’t see to me behind the wall. I did a lot of search in google but i can’t find it and can’t fix my script.

Here is my script. Thanks and sorry for my bad english.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{

    public float speed;

    private float waitTime;

    public float startWaitTime;

    public Transform[] moveSpot;
    private int randomSpot;

    private Transform myTransform;
    public Transform target;
    public int moveSpeed;

    

    void Awake()
    {
        myTransform = transform;
    }

    void Start()
    {
        waitTime = startWaitTime;
        randomSpot = Random.Range(0, moveSpot.Length);

        target = GameObject.FindGameObjectWithTag("Player").transform;
        
    }

    
    void Update()
    {
         if (Vector2.Distance(transform.position, target.position) < 5f)
        {

            transform.position = Vector2.MoveTowards(transform.position, target.position, moveSpeed * Time.fixedDeltaTime);
            Vector3 dir = target.position - transform.position;
            float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg + 90f;
            transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        }

        Debug.DrawLine(target.position, myTransform.position, Color.red);

        Patrol();

    }

    public void Patrol()
    {
        transform.position = Vector2.MoveTowards(transform.position, moveSpot[randomSpot].position, speed * Time.deltaTime);

        Vector3 dir = moveSpot[randomSpot].position - transform.position;

        float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg + 90f;
        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

        if (Vector2.Distance(transform.position, moveSpot[randomSpot].position) < 0.2f)
        {

            if (waitTime <= 0)
            {
                randomSpot = Random.Range(0, moveSpot.Length);
                waitTime = startWaitTime;

            }
            else
            {
                waitTime -= Time.deltaTime;

            }


        }
    }

}

Hi!

Basically, what you want to do is to detect if there is any Collider between your Player and Enemy.

To do that you can either use Physics.Linecast ot Physics2D.Linecast (since I am not sure is you are using 3D but top-down 2D-ish or simple 2D, so here is both documentation for any of them: Unity - Scripting API: Physics.Linecast Unity - Scripting API: Physics2D.Linecast ).


What it does - it casts line between 2 given points and detects if there is any Collider on the path. Therefore keep in mind that your obstacle MUST have Collider attached.

If you want to specify certain objects behind which player can hide you can use tag and when receive any collider from Linecast use CompareTag (Unity - Scripting API: Component.CompareTag) to check if Collider has correct Tag.


It would look something like this, for example:

void Update()
{
    RaycastHit hit;

    if (Physics.Linecast(player.position, enemy.position, out hit))
    {
        if (hit.collider.CompareTag("hide"))
        {
            // Stop chasing
        }
    }
}

Hope that will help you :slight_smile: