- Home /
Making a turret fire another arrow if an enemy survives the hit
Hey, I made a script which is attached to a turret,
public class TurretRealScript : MonoBehaviour {
public GameObject arrowPrefab; public GameObject Target;
void Start () {
}
// Update is called once per frame void Update () {
} void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "Enemy") { Target = other.gameObject; Instantiate(arrowPrefab, transform.position,transform.rotation); } }
}
and a script attached to the object which is to be fired at the enemy once the collision is triggered
public class ArrowProjectileScript2 : MonoBehaviour {
public GameObject Target;
private Vector3 direction = Vector3.forward;
public float speed=30.0f;
// Use this for initialization void Start () {
}
// Update is called once per frame void Update () { Target = GameObject.FindWithTag("Enemy"); transform.Translate(direction*speed*Time.deltaTime); transform.LookAt(Target.transform); }
}
So far this works fine, when the turret sphere detects a collision the arrow is fired at the enemy and my script attached to the enemy destroys the arrow on collision but must require a further 2 hits before the enemy itself is destroyed,
public class Enemy1DestroyScript : MonoBehaviour {
public int count=0; // Use this for initialization void Start () {
}
// Update is called once per frame void Update () { if (count>2) { Destroy(gameObject); } } void OnCollisionEnter(Collision hit) { if(hit.gameObject.tag=="Arrow") { Destroy(hit.gameObject); count++; } }
}
What I would like to happen is after the first 2 secs of the arrow being fired, if there is an enemy still alive in the collison area then fire another arrow. currently an arrow is only fired when an a new enemy triggers the onTriggerEnter which is no use if some enemies require more than one arrow to be killed.
Thanks for your time!
Answer by cuddlybunny · Jun 01, 2012 at 02:34 PM
var Target : GameObject;
var bullet : GameObject;
var fired = 0;
function Start () {
}
function Update () {
if (Target){
fireagain ();
}
}
function OnTriggerEnter (other : Collider) {
if (other.gameObject.tag == "enemy"){
Target = other.gameObject;
Instantiate(bullet,transform.position,transform.rotation);
fired = 1;
}
}
function fireagain (){
if (fired == 0){
fired = 1;
Instantiate(bullet,transform.position,transform.rotation);
yield WaitForSeconds (1);
fired = 0;
}
}
Answer by cuddlybunny · Jun 01, 2012 at 02:01 PM
Its Java but few changes should make it work.
var Target : GameObject;
var bullet : GameObject;
var fired = 0;
var firerate = 2;
function Start () {
}
function Update () {
if (Target)
{
fireagain ();
}
}
function OnTriggerEnter (other : Collider) {
if (other.gameObject.tag == "enemy"){
Target = other.gameObject;
fireagain ();
}
}
function fireagain (){
if (fired == 0)
{
fired = 1;
Instantiate(bullet,transform.position,transform.rotation);
yield WaitForSeconds (firerate);
fired = 0;
}
}
Answer by fafase · Jun 01, 2012 at 12:28 PM
OnTriggerEnter only returns true on the frame when the enemy is entering the collider.
If you have new enemies instantiated at runtime, you can launch a function that would check for new enemies with FindGameObjectsWithTag, I would recommend to use every 1 or 2 sec as it is a very expensive function.
If they are all on the map at the beginning, just check for them in the Start function
Once you have those enemies in a list or built-in array of game object, you can check the distance with Vector3.Distance and check if the enemies are in a define distance and attack the closest one. Once the closest one is dead, the turret will check again for closest one and so on.