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 Hyperion · Dec 16, 2013 at 02:29 AM · gameobjectdynamicfindlaserclosest

Dynamic Laser Fence Scripting Problem

Hi Everyone,

I have a player that starts out on an empty terrain. He can place turrets one by one that should shoot a laser at the closest turret once in place. This is to create a laser fence. The turrets used to shoot their lasers, but when I didn't have the following script, they all shot at the second turret created. Thus I implemented this script, but now, the turrets won't shoot anything at each other!

Here is my script for the finding of the closest turret:

 //..........Finding Closest Turret
     var nearestDistanceSqr = Mathf.Infinity;
     var taggedGameObjects: GameObject[] = GameObject.FindGameObjectsWithTag("FenceLaserPoint1");
     var nearestTrt : Transform = null;
  
         // loop through each tagged object, remembering nearest one found
     for (var obj : GameObject in taggedGameObjects) {
  
         var objectPos = obj.transform.position;
         var distanceSqr = (objectPos - transform.position).sqrMagnitude;
  
         if (distanceSqr < nearestDistanceSqr) {
             nearestTrt = obj.transform;
             nearestDistanceSqr = distanceSqr;
         }
     }
     //................................

And here is my laser shooting code:

 Laser=Instantiate(laserPrefab) as LineRenderer;
         if (Laser!=null)
         {
             Laser.SetPosition(0,thisLaserPoint.position);
             Laser.SetPosition(1,nearestTrt.transform.position);
             Destroy(Laser, laserEnergy);
         }

I know for sure that the laser shooting code is working well. It's the scanning code to find the closest turret that isn't working. Except, there are no errors so I cannot pinpoint any specific issues. I would greatly appreciate if you help me solve this problem. Thank you,

Hyperion

Comment
Add comment · Show 3
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 iwaldrop · Dec 16, 2013 at 02:48 AM 0
Share

Are you sure that anything is being found at all? Are there definitely GameObjects in the scene with the tag "FenceLaserPoint1"? What does nearestDistanceSqr read when the loop is done?

avatar image Hyperion · Dec 16, 2013 at 02:59 AM 0
Share

I'm not sure anything is being found, but previously the code could find objects with that tag. At the start, there are no objects with that tag (and it is not misspelled, I checked). But the player creates them. So there will be objects after they are created. Do you think that is the problem?

nearestDist... prints out 6.0blablabla, and changes by a millionth every time a new turret is created.

avatar image Hyperion · Dec 16, 2013 at 03:04 AM 0
Share

DISCOVERY: I commented out 'nearestDistanceSqr = distanceSqr' and all the turrets started shooting at the last turret created. Here, nearesDistsqr reads "Infinity", always. This is still not what I want, though.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by iwaldrop · Dec 16, 2013 at 04:19 AM

In all honesty I think what you're doing is just a little overly complicated. I'd just do something like this:

 public static class Tools
 {
     public static T FindNearest<T>(Transform reference) where T : MonoBehaviour
     {
         return FindNearest<T>(reference.position);
     }

     public static T FindNearest<T>(Vector3 worldPosition) where T : MonoBehaviour
     {
         List<T> list = GameObject.FindObjectsOfType<T>().ToList();
             list.Sort(
             (x, y) =>
             (x.transform.position - reference).sqrMagnitude
             .CompareTo((y.transform.position - reference).sqrMagnitude));
         return list.First();
     }
 }

To use it, you just pass a Type and a Transform, like so:

 Turret nearestTurret = Tools.FindNearest<Turret>(transform.position);

Maybe that's one of the advantages of using C#, because I'm not sure if that works in US or not.

Comment
Add comment · Show 30 · 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
avatar image Hyperion · Dec 16, 2013 at 05:17 AM 0
Share

Well, I'll try to convert my script into C# and then add this in, and update you asap (in a few days). Thanks for the script. Could you please tell me the logic?

avatar image iwaldrop · Dec 16, 2013 at 05:19 AM 0
Share

Fundamentally it's just a LINQ query in a method that returns the first object in a sorted list.

What I'm doing in the above is finding all objects of type Turret (so you may have to change that bit in your implementation) then sorting them by the sqr$$anonymous$$agnitude (because it's faster than magnitude).

avatar image Hyperion · Dec 16, 2013 at 05:22 AM 0
Share

Okay, thanks.

avatar image iwaldrop · Dec 16, 2013 at 05:39 AM 0
Share

A couple notes/disclaimers about this. The above code does not discover GameObjects via tags, but by Type ins$$anonymous$$d. Also, something like this is fine to run every now and again, but should not be used every frame.

avatar image Hyperion · Dec 16, 2013 at 06:07 AM 0
Share

I'm fine with it being used every once in a while, but what does "Type" mean? Will this be able to differentiate between different instances of one object?

Show more comments

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

17 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

Related Questions

How do I find the closest target with a tag? c# 2 Answers

how to find the closest (and second closest) object with tag - one object 4 Answers

GameObject.Find closest ?? 2 Answers

All the objects in my script choose the same target. 2 Answers

Move to closest object(c#) 2 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