I have an enemy shooting around. My game mechanic involves hovering over the enemy with the mouse to drain its health. While the enemy is moving, you need to follow it with your mouse. If you don't succeed to follow it and the mouse leaves the enemy's collider, the enemy's health should be refilled.
The trouble is that the enemies' bullets have also a collider, and OnMouseOver acts weird when more colliders appear on top of each other.
How can I only select a particular Layer for the enemy to detect only that layer without acknowledging the bullets layer?
My Code so far:
public class FightDarkalls : MonoBehaviour
{
[Header("Guidance Light On?")]
public bool _guidanceLight = false;
[Header("Target Settings")]
public GameObject _monster;
public GameObject _targetEffect;
public Animator _TargetEffectAnim;
public LayerMask _monsterLayer;
int _layer;
[Space]
[Header("Damage Darkall")]
public float damage = 0.1f;
public float damageSpeed = 2.5f;
[Header("OBJs to deactivate when Darkall dead")]
public GameObject[] _deactivateOBJsOnDeath;
[Header("Health Settings")]
public GameObject _healthBar;
public float startingHealth;
public float health;
public GameObject _deathDarkallParticle;
//Animator
private Animator _anim;
Vector2 mousePosition;
public LayerMask _interactLayer;
private void Start()
{
_targetEffect.SetActive(false);
_anim = GetComponent<Animator>();
_TargetEffectAnim = _targetEffect.GetComponent<Animator>();
}
private void Update()
{
CheckIfGuidanceIsActive();
_healthBar.transform.localScale = new Vector2(health, 1f);
_deathDarkallParticle.transform.position = Vector2.Lerp(_deathDarkallParticle.transform.position, _monster.transform.position, 100f * Time.deltaTime);
SetMousePosition();
}
void CheckIfGuidanceIsActive()
{
if (GameObject.Find("_GuidanceLight").GetComponent<FollowCursor> ()._guidanceActivated == true)
{
_guidanceLight = true;
}
else
_guidanceLight = false;
}
private void OnDrawGizmos()
{
Gizmos.DrawSphere(mousePosition, 1f);
}
private void OnMouseOver()
{
if (_guidanceLight)
{
//[DAMAGE]\\
health -= damage * Time.deltaTime * damageSpeed;
_anim.SetBool("TakeDamage", true);
//Activate Target Effect
_targetEffect.SetActive(true);
_TargetEffectAnim.SetBool("OnDarkallOver", true);
//Damage the target
//Kills the target
if (health <= 0f)
{
foreach (GameObject _dOBJs in _deactivateOBJsOnDeath)
{
_dOBJs.GetComponent<SpriteRenderer>().enabled = false;
}
//Change Cam Shake to another OBJ
GameObject.Find("Main Camera").GetComponent<CameraShake>().CamShakeEffect(0.1f, 0.5f);
health = 0f;
_deathDarkallParticle.SetActive(true);
_deathDarkallParticle.GetComponent<ParticleSystem>().Play();
_monster.gameObject.SetActive(false);
Destroy(_monster.gameObject, 3f);
}
}
}
private void OnMouseExit()
{
_anim.SetBool("TakeDamage", false);
//If failed to kill, reset health
health = startingHealth;
//Deactivate Target
_targetEffect.SetActive(false);
_TargetEffectAnim.SetBool("OnDarkallOver", false);
}
void SetMousePosition()
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
}