Hello, I have a Problem with my Code.
In my code, a specific section is only triggered when im in debugging mode.
This means that during normal gameplay, this part of the code is never called. I tried debugging it, and always when im debugging, the code does what it should be.
As soon as I remove the breakpoints, the code dosent trigger.
I tried to test this with “Debug.log” so I can see if it triggers when im not debugging.
In Debugging it wrote out the log, in normal mode, it didnt.
Am I using the update() method wrong?
My Class:
public class MonsterAI : MonoBehaviour {
[Header("Movement Settings")]
[SerializeField] protected float moveSpeed = 3.5f;
[SerializeField] protected Transform[] patrolPoints;
[Header("Detection and Attack Settings")]
[SerializeField] protected float detectionRange = 10f;
[SerializeField] protected float chaseRange = 15f;
[SerializeField] protected float attackRange = 1.5f;
[SerializeField] protected float attackCooldown = 2f;
[Header("Audio Settings")]
[SerializeField] protected List<AudioClip> alertSounds;
[SerializeField] protected AudioClip attackSound;
protected NavMeshAgent agent;
protected Animator animator;
protected int currentPatrolIndex;
protected bool isChasing;
protected Rigidbody rigidBody;
protected bool canAttack = true;
protected Transform player;
protected AudioSource audioSource;
private bool audioAlreadyPlayed = false;
protected virtual void Start() {
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
rigidBody = GetComponent<Rigidbody>();
currentPatrolIndex = 0;
rigidBody.isKinematic = true;
// Player Referenz automatisch setzen
GameObject playerObject = GameObject.FindWithTag("Player");
if (playerObject != null) {
player = playerObject.transform;
} else {
Debug.LogError("Player object not found.");
}
// AudioSource-Komponente hinzufügen, falls nicht vorhanden
audioSource = GetComponent<AudioSource>();
if (audioSource == null) {
audioSource = gameObject.AddComponent<AudioSource>();
}
GoToNextPatrolPoint();
}
protected virtual void Update() {
if (player == null) {
return;
}
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
if (isChasing) {
if (distanceToPlayer > chaseRange) {
isChasing = false;
GoToNextPatrolPoint();
audioAlreadyPlayed = false;
} else {
ChasePlayer();
if (distanceToPlayer <= attackRange && canAttack) {
Attack();
Debug.Log("Zyklop greift an");
}
}
} else {
if (distanceToPlayer < detectionRange) {
isChasing = true;
PlayRandomAlertSound(audioAlreadyPlayed);
}
Patrol();
}
UpdateAnimator();
}
protected virtual void Patrol() {
if (agent != null && agent.isActiveAndEnabled && agent.isOnNavMesh) {
if (agent.remainingDistance < 0.5f && !agent.pathPending) {
GoToNextPatrolPoint();
}
}
}
protected virtual void GoToNextPatrolPoint() {
if (patrolPoints.Length > 0) {
agent.destination = patrolPoints[currentPatrolIndex].position;
currentPatrolIndex = (currentPatrolIndex + 1) % patrolPoints.Length;
}
}
protected virtual void ChasePlayer() {
agent.SetDestination(player.position);
}
protected virtual void UpdateAnimator() {
bool isWalking = agent.velocity.magnitude > 0.1f;
animator.SetBool("IsWalking", isWalking);
}
protected virtual void Attack() {
canAttack = false;
PlayAttackSound();
StopMoving();
StartCoroutine(AttackCooldown());
}
protected IEnumerator AttackCooldown() {
yield return new WaitForSeconds(attackCooldown);
canAttack = true;
}
protected void PlayAttackSound() {
if (attackSound != null) {
audioSource.PlayOneShot(attackSound);
}
}
protected void PlayRandomAlertSound(bool audioAlreadyPlayed) {
if (alertSounds != null && alertSounds.Count > 0 && !audioAlreadyPlayed) {
int randomIndex = Random.Range(0, alertSounds.Count);
AudioClip randomAlertSound = alertSounds[randomIndex];
audioSource.PlayOneShot(randomAlertSound);
audioAlreadyPlayed = true;
}
}
private void StopMoving() {
if (agent.isActiveAndEnabled && agent.isOnNavMesh) {
agent.isStopped = true;
agent.velocity = Vector3.zero;
}
animator.SetBool("IsWalking", false);
}
And this is the specific code that isnt triggert as soon as Im not im debugging mode:
if (distanceToPlayer <= attackRange && canAttack) {
Attack();
Debug.Log("Zyklop greift an");
}