- Home /
Make a turret shoot multiple targets
hi, i got the turret script from the FPS tutorial :
var attackRange = 30.0;
var shootAngleDistance = 10.0; var target : Transform;
function Start () { if (target == null && GameObject.FindWithTag("Player")) target = GameObject.FindWithTag("Player").transform; }
function Update () { if (target == null) return;
if (!CanSeeTarget ()) return;
// Rotate towards target
var targetPoint = target.position; var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
// If we are almost rotated towards target - fire one clip of ammo var forward = transform.TransformDirection(Vector3.forward); var targetDir = target.position - transform.position; if (Vector3.Angle(forward, targetDir) < shootAngleDistance) SendMessage("Fire");
}
function CanSeeTarget () : boolean { if (Vector3.Distance(transform.position, target.position) > attackRange) return false;
var hit : RaycastHit; if (Physics.Linecast (transform.position, target.position, hit)) return hit.transform == target;
return false;
}
It worked fine on searching the object tagged, but if there is more than 1 same object tagged "player", it simply kills one of them and rests(like freezing) there. How do i make the turret randomly select 1 of them, shoot it until it dies and switch to another target? THANKS!
Answer by William Cain · Mar 31, 2012 at 02:56 AM
What I did was take the code that is in the Start() function and put it into the update function. Once it is killed target becomes null and it sets a new target.
function Update () { if (target == null && GameObject.FindWithTag("Player")) target = GameObject.FindWithTag("Player").transform; }
Your answer
Follow this Question
Related Questions
Attack multiple targets in the same tag? 1 Answer
Destroying gameObject destroys all objects 2 Answers
Tank Turret move on XAxis by Mouse XAxis 1 Answer
I need a turret script 4 Answers
How to TAG the Player? 1 Answer