Hello guys.
Here I’m having a bit of an issue. I’ve got this simple script that sets a bool true if an enemy (with a collider and a rigidbody) is in the range of the collider. It works just fine but sometimes, when I move a bit, the bool just go false even if the enemy stays within the range of the collider…
Any ideas why it happens?
the scripts :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttacker : MonoBehaviour
{
AnimatorManager animatorManager;
public Collider sphereCollider;
private Transform target;
public bool isTargetting;
private void Awake()
{
animatorManager = GetComponent<AnimatorManager>();
}
private void Update()
{
if (target == null)
isTargetting = false;
else
isTargetting = true;
}
public void HandleAttack()
{
animatorManager.PlayTargetAnimation("Female Sword Attack 1", true, true);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
target = other.transform;
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Enemy"))
{
target = null;
}
}
public void HandleTargetting()
{
if (target == null)
{
return;
}
transform.LookAt(target);
}
}
here a screenshot of the running game where we can see the “isTargetting” bool set to false when the enemy is clearly in the collider :
Thx in advance for the help