- Home /
Turret stops firing when enemy is out of range
So currently I want my turret to fire if the "monster" tag is within 500 yds (as an example). It will correctly do this, but if it doesn't kill the monster while it's still in it's turret range it will stop firing and not fire anymore.. I spawn 30 'monsters' all with the "monster" tag. Should I rename the tags for each monster?? What it's currently doing is not switching targets when one is out of range. I'm not sure what I could do to fix this.
function Update () {
//if (Timer < Time.time) {
if (GameObject.FindWithTag("monster")) {
var distance = Vector3.Distance(this.gameObject.transform.position, GameObject.FindWithTag("monster").transform.position);
if (distance < 500 && GameObject.FindWithTag("monster")) {
missle = Instantiate(projectile, this.transform.position, this.transform.rotation) as GameObject;
missle.transform.position.y = this.transform.position.y + 70;
missle.transform.position.z = this.transform.position.z + 100;;
missle.transform.localScale += Vector3(100,100,100);
missle.tag = "missle";
missle.AddComponent("Rigidbody");
missle.AddComponent("FireMissles");
missle.AddComponent("SphereCollider");
}
}
//Timer = Time.time + 1;
}
}
This is attatched to the 'missle' object
#pragma strict
public class FireMissles extends MonoBehaviour {
var targetTag : String;
var target : Transform;
var speed : float = 500;
function Start () {
var tar = GameObject.FindWithTag("monster");
if (tar) {
target = tar.transform;
}
}
function Update () {
transform.LookAt(target);
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
}
So you're saying that it just stops shooting if the monster isn't killed?
Yeah, if the monster is outside of the 500 yd distance, and doesn't have 0 hp. The turret will just stop firing. It's because it calculates distance based on the first monster tag it sees.
First you going to have to define your targeting criteria. Do you shoot at the monster, or do stick with one monster until it is out of range for example. Second you will need to expand your script to search out targets that meet that criteria. A common approach is to use GameObject.FindGameObjectsWithTag() to get a list of monsters. Then you can search through that list for ones that meet that criteria. A second common approach is to use Physics.OverlapSphere() to get a list of colliders that are within range and use that list to evaluate targets. Third, you'll have to communicate the target to the missile, The easiest solution would be for the firing script to set 'target' rather than have it done by the missile in its Start().
What robertbu said is right.
If you're doing the target search in every update, I'd recommend putting all eligible targets into an array or List that you update whenever a target dies or is spawned. It'll be good for performance whatever you come up with as the targeting criteria.
Also a smaller performance tweak everyone should use when comparing distances is not to use Vector3.Distance unless you really have to know the distance :) If you're comparing 2 distances to find the greater one (like here), it's better to ins$$anonymous$$d of this:
const float range = 500F;
public Vector3 turretPos;
public Vector3 targetPos;
void Update()
{
if (Vector3.Distance(turretPos, targetPos) < range)
{
// is in range
}
}
do
const float range = 500F;
const float rangeSQ = range * range;
public Vector3 turretPos;
public Vector3 targetPos;
void Update()
{
if ((turretPos - targetPos).sqr$$anonymous$$agnitude < rangeSQ)
{
// is in range
}
}
You'll get rid of one square root calculation since you don't need to know the actual distance from turret to target, just compare magnitude.
Answer by EvilTak · Apr 21, 2014 at 08:32 AM
You can add a sphere collider to your turret and set it as a trigger. Then you can use this method:
var isAiming = false;
var target : Transform;
function OnTriggerEnter(var collEnter : Collider)
{ if(!isAiming)
{ isAiming = true;
target = collEnter.transform;
}
}
function OnTriggerStay(var collStay : Collider)
{ if(isAiming && collStay.transform == target)
{
//shoot;
}
}
function OnTriggerExit(var collExit : Collider)
{ if(isAiming && collExit.transform == target)
{ isAiming = false;
target = null;
}
}
This is if you want to aim at one monster at a given time.
Your answer
Follow this Question
Related Questions
Fire turret "projectile" from origin to end 0 Answers
How to check if a trajectory is blocked? 1 Answer
Issues with one object receiving information from one of many other objects 1 Answer
Possibility of adding More than One Projectile 1 Answer
Getting 2D projectiles that fire in the direction of the mouse position to work in unity 5? 0 Answers