#Enemy not attacking

1 messages · Page 1 of 1 (latest)

sage elk
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeeleEnemy : MonoBehaviour
{
    [Header("Attack Parameters")]
    [SerializeField] private float attackCooldown;
    [SerializeField] private float range;
    [SerializeField] private int damage;

    [Header("Collider Parameters")]
    [SerializeField] private float colliderDistance;
    [SerializeField] private BoxCollider2D boxCollider;

    [Header("Player Layer")]
    [SerializeField] private LayerMask playerLayer;
    private float cooldownTimer = Mathf.Infinity;

    private Animator anim;


    private void Awake()
    {
        anim = GetComponent<Animator>();
    }


    private void Update()
    {
        cooldownTimer += Time.deltaTime;
        if (PlayerInSight())
        {
            if (cooldownTimer >= attackCooldown)
            {
                cooldownTimer = 0;
                anim.SetTrigger("Attack");
                Debug.Log("Attack");
            }
        }
    }

    private bool PlayerInSight()
    {
        RaycastHit2D hit =
            Physics2D.BoxCast(boxCollider.bounds.center + transform.right * range * transform.localScale.x * colliderDistance,
            new Vector3(boxCollider.bounds.size.x * range, boxCollider.bounds.size.y, boxCollider.bounds.size.z),
            0, Vector2.left, 0, playerLayer);
             return hit.collider != null;
    }
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireCube(boxCollider.bounds.center + transform.right * range * transform.localScale.x * colliderDistance,
            new Vector3(boxCollider.bounds.size.x * range, boxCollider.bounds.size.y, boxCollider.bounds.size.z));
    }


}
#

player inspector ^^^^^^^^^^^^^

#

enemy inspector ^^^^^^^^^

sage elk
#

@buoyant lynx

#

where are u

buoyant lynx
#

yeah yea

sage elk
#

oh hello

sage elk
buoyant lynx
#

try changing the value of cooldown timer

sage elk
#

@stuck trout

buoyant lynx
#

@sage elk

#

You can't use enemy AI?

sage elk
sage elk
#

@buoyant lynx do u even know what is the error

buoyant lynx
#

no

#

but enemy AI makes it easy

sage elk
#

no no

#

i want this

buoyant lynx
#

wha'ts Physics2D.BoxCast?

sage elk
#

to create a box

buoyant lynx
#

unity doesnt have any doccumentation regarding it

sage elk
#

to check if the player is in sight

buoyant lynx
#

Its Vector 2 for box cast

#

atleast in teh docs

sage elk
#

in the tutorail it works

#

but not for me

buoyant lynx
#

which tutorial?

sage elk
buoyant lynx
#

try sswitching the vector 3 for vector2

sage elk
#

no i cant do that cuz the z position is needed

sage elk
buoyant lynx
#

patience

#

Any errors that unity throws?

sage elk
#

nope

#

@stuck trout

stuck trout
#

stop pinging me

sage elk
stuck trout
#

no

sage elk
#

anyone