Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cj1098 · Apr 20, 2014 at 07:31 PM · projectileturret

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);
 }
 
 
 
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ToxxicSin · Apr 20, 2014 at 08:23 PM 0
Share

So you're saying that it just stops shooting if the monster isn't killed?

avatar image cj1098 · Apr 20, 2014 at 08:26 PM 0
Share

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.

avatar image robertbu · Apr 20, 2014 at 08:43 PM 1
Share

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().

avatar image NoseKills · Apr 21, 2014 at 08:45 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges