- Home /
Auto Targeting script issue
Hi everyone, I'm trying to have bullets auto target. So far I have it working on all targets but when target gets destroyed and when new target selected bullet no longer are auto targeting. Can someone please help here is the Js script:
var target : GameObject;
var targetAlive : boolean = false;
function Update () {
if(Enemy_Collisions.e_alive)
{
if(targetAlive == true)
{
var new_target : Vector3 = target.transform.position - transform.position;
rigidbody.velocity = new_target.normalized * 100;
transform.LookAt(target.transform.position);
}
}
if(!Enemy_Collisions.e_alive)
{
targetAlive = false;
}
}
Hi, for your algo to work, both Enemy_Collisions.e_alive
and targetAlive
must be equal to true
. So when do you passed them to true? Can you show us more code? Thanks
Answer by Ericool · Apr 06, 2015 at 01:19 PM
make sure that target is never null otherwise that functionnality must be disabled . What you can do is calling a coroutine for each bullet instantiated , that coroutine is simply lookat(target) at each second for instance. Then it moves forward , until the distance between the bullet and the target is small enough , then call auto bomb and destroy that bullet . That could be a start.
Your answer
![](https://koobas.hobune.stream/wayback/20220613192155im_/https://answers.unity.com/themes/thub/images/avi.jpg)