i want to make my turret attack multiple enemies one by one
My turret just attacks at the first spawn enemy then keep on shooting at the same position even that enemy has been destroyed, I want it to attack other enemies also. My turret works fine with one enemy even if the enemy moves it turns around and destroy it but fails for multiple enemies Here is the code for the bullet public class bulletforblaster : MonoBehaviour {
public Rigidbody projectile;
public float speed = 20;
private int counter=0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
counter ++;
if (counter > 61)
{
counter = 0;
}
if(counter>60)
{
Rigidbody instantiatedProjectile = Instantiate(projectile,
transform.position,
transform.rotation)
as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
}
}
} Here is the code for the turret
using UnityEngine;
using System.Collections;
public class Turret_movement : MonoBehaviour {
public Transform Lookatobject;
void Start () {
}
// Update is called once per frame
void Update () {
transform.LookAt (Lookatobject);
}
Here is the code inserted at the empty object for the bullet
using UnityEngine;
using System.Collections;
public class Blaster_destroy_object : MonoBehaviour {
void OnTriggerEnter(Collider other) {
switch(other.gameObject.tag)
{
case "enemy":
DestroyObject(other.gameObject);
DestroyObject(gameObject);
GameObject monster3 = (GameObject)Instantiate(Resources.Load("DamageText"),gameObject.transform.position,Quaternion.identity);
break;
case "wall":
DestroyObject(gameObject);
break;
}
}
}
I am new in the world of unity so if possible explain in detail . Any help is appreciated
Answer by Trigary · Jan 29, 2016 at 04:53 PM
I'm not good at understanding others' codes, but in the 6th line of the turret script you specify the enemy, that variable won't change, therefore the turret will always look at that enemy's starting position which was dragged into that "variable" in Unity.
yeah true, but i have no idea how to make the turret rotate towards the newly spawned enemy, is there any other method besides transform.LookAt (Lookatobject); to complete my task. thanks in advance for the solution or whatever help u can give.
Raycasts are a great way to detect cone of vision and things like that, make the turret send a ray in front of him, if that hits nothing, make the turret rotate. If it does, make the turret fire.