hello, i am pretty new to coding and im trying to make a basic game where you fight enemies, but my code isnt working and i dont know why, i have a box collider set to trigger in front of the enemy, if the collider is triggered, it disables a bool variable called “walking”, and the enemy can only move if walking is true, walking is set to true on void OnTriggerExit, bt for some reason, the enemy keeps walking when the collider is triggered and its trying to play bot the walking and attacking animation at the same time, here is the code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.VisualScripting;
using UnityEngine;
public class enemyBehaviour : MonoBehaviour
{public GameObject player;
public float speed;
public float distance;
public float attackDistance;
public float timer;
private bool triggered;
private bool attackMode;
private bool inRange;
private bool walking;
public float distanceBetween;
public float health;
public bool playerNoticed;
private bool timering;
private bool isWalking;
Animator animator;
private Rigidbody2D rb;
SpriteRenderer spriteRenderer;
void Start()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
walking = false;
}
void Update()
{
timer -= Time.deltaTime;
if (timer < 0){
timer = 3;
timering = true;
}
distance = Vector2.Distance(transform.position, player.transform.position);
Vector2 direction = player.transform.position - transform.position;
direction.Normalize();
if (distance < distanceBetween && walking == true && timering == false){
transform.position = Vector2.MoveTowards(this.transform.position, player.transform.position, speed * Time.deltaTime);
animator.Play("ewalkp1");
timering = false;
}
{
}
if (health <= 0) {
gameObject.SetActive(false);
}
{
}
}
void OnTriggerEnter2D (Collider2D col){
animator.SetBool("inRange", true);
animator.Play("eattackp1");
animator.SetBool("walking", false);
walking = false;
}
void OnTriggerExit2D (Collider2D col){
animator.SetBool("inRange", false);
animator.SetBool("walking", true);
walking = true;
timering = false;
}
}
sorry if my code is hard to read, thank you to anyone who will help this newbie