Hi!
I don’t find a right title, sorry for that.
The problem is, that i have an automatic turret, wich targets enemies in range and attack.
That works very well but i will click on the enemy too, to make some damage (its a defense like game).
If the turret target on enemy i can’t click on them. The OnMouseDown function will not work.
Enemy Hitbox Script:
using UnityEngine;
using System.Collections;
public class hitbox : MonoBehaviour
{
public GameObject go;
private BasicEnemy g;
private float life = 2;
//Start function
public void Start()
{
g = (BasicEnemy)go.GetComponentInParent(typeof(BasicEnemy));
}
public void damage(float amount)
{
g.onHit();
if (amount > 0)
{
if((life - amount) <= 0)
{
destroy ();
}
else
{
life = life - amount;
}
}
}
void Update()
{
if(life <= 0)
{
destroy ();
}
}
public void setLife(float amount)
{
if(amount > 0)
{
life = amount;
}
}
void OnMouseDown()
{
damage (1);
}
void destroy()
{
Destroy(go);
}
public float getLife()
{
return life;
}
}
Turret Script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BasicTurret : MonoBehaviour, TurretInterface
{
public Transform gunEnd;
protected float shootRate;
protected float shootRange;
public float shootDamage;
protected float nextFire;
protected hitbox enemy;
protected SphereCollider myCollider;
protected LineRenderer laserLine;
protected Queue<hitbox> enemyQueue = new Queue<hitbox>();
// Use this for initialization
public void Start ()
{
shootRange = 4.5f;
shootRate = 0.5f;
shootDamage = 1;
laserLine = GetComponent<LineRenderer>();
myCollider = transform.GetComponent<SphereCollider> ();
myCollider.radius = shootRange;
}
// Update is called once per frame
public void FixedUpdate ()
{
if(enemy != null)
{
this.gameObject.transform.LookAt (enemy.transform);
}
else
{
laserLine.enabled = false;
if (enemyQueue.Count > 0)
{
if (enemyQueue.Peek () != null)
{
enemy = enemyQueue.Dequeue ();
this.gameObject.transform.LookAt (enemy.transform);
}
}
}
}
protected void Update()
{
if (enemy != null && Time.time > nextFire)
{
nextFire = Time.time + shootRate;
laserLine.SetPosition (0, gunEnd.position);
laserLine.SetPosition (1, enemy.transform.position);
StartCoroutine (attack ());
}
}
public void setRange(float amount)
{
if(amount > 0)
{
shootRange = amount;
}
}
public void setRate(float amount)
{
if(amount > 0)
{
shootRate = amount;
}
}
public void setDamage(float amount)
{
if (amount > 0)
{
shootDamage = amount;
}
}
public void OnTriggerEnter(Collider other)
{
if (enemy == null)
{
if (other.gameObject.GetComponentInParent<BasicEnemy> ())
{
enemy = other.gameObject.GetComponent<hitbox> ();
this.gameObject.transform.LookAt (enemy.transform);
}
}
else
{
enemyQueue.Enqueue (other.gameObject.GetComponent<hitbox>());
}
}
IEnumerator attack()
{
yield return new WaitForSeconds (0.02f);
enemy.damage (shootDamage);
laserLine.enabled = true;
yield return new WaitForSeconds (0.08f);
laserLine.enabled = false;
}
public void OnTriggerExit (Collider other)
{
if(enemy != null)
{
if(other.gameObject == enemy)
{
enemy = null;
}
}
}
}
I hope u understand my problem xD