Enemy following player with walls between

44 Views Asked by At

I'm trying to make a script where the enemy follows the player in a 2.5D world when he can see him. I tried to use a raycast in every direction, but the collision with the player (and the walls) only worked when the object was directly in it. Thats why i think that the raycast collides with the enemy itself but I didn't made to manage it otherwise

[SerializeField] private float visualRange = 1000.0f;
[SerializeField] private LayerMask minotaur;

private ContactFilter2D filter;

private void Update() {
    MoveToPlayer();
}

private void MoveToPlayer() {
    for(int i = 0; i <= 360; i += 5) {
        List<RaycastHit2D> results = new List<RaycastHit2D>();
        filter.layerMask

        int number = Physics2D.Raycast(transform.position, new Vector3(0, 0, i), filter, results, visualRange);

        foreach (RaycastHit2D hit in results) {
            if (hit.collider.tag == GameObject.FindGameObjectWithTag("Player").tag) {
                Debug.Log("Collision Player");
            }
        }
    }
}

Thats the newest solution i tried, the filter is empty because I don't know how to add two layers.

As i mentioned i try it with a filter, but i don't know how to make the filter so it would work. I also tried to use the z-coordinates but that also doesn't work well.

0

There are 0 best solutions below