Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Tricell · Feb 17, 2016 at 07:14 PM · arraysangletransforms

I am having an issue with vector 3 angles when multiple game objects are involved

This reference from unity works perfectly; however, it only works when trying to detect the angle of a single game object. Is there some way to make this into an array, so that it detects whether or not any game objects are within a certain degrees? As of now, it only detects the angle of a single game object and ignores any others. C# only please, THANKS!!!!

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour {
     public Transform target;
     void Update() {
         Vector3 targetDir = target.position - transform.position;
         Vector3 forward = transform.forward;
         float angle = Vector3.Angle(targetDir, forward);
         if (angle < 5.0F)
             print("close");
         
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Justice-V18 · Feb 18, 2016 at 12:30 PM

You could use a for loop to achieve this:

  using UnityEngine;
  using System.Collections;
  
  public class ExampleClass : MonoBehaviour {
 public Transform[] target;//add all objects here
     private int i;//Made it global so when I can make it 0 easily and start for loop again.
     float angle;
 
     void Update() 
     {
         for (i = 0; i < target.Length; i++)//go from through whole array.
         {
             Vector3 targetDir = target [i].position - transform.position;
             Vector3 forward = transform.forward;
 
             angle = Vector3.Angle (targetDir, forward);
 
             if (angle < 5.0F) 
             {
                 print ("Close" + target [i].name);
             }
         }
 
         if(i == target.Length - 1)//This happens after the for loop has gone through all of them.
             i = 0;//rest "i" back to 0 so we can start the for loop again.
     }
 }
Comment
Add comment · Show 2 · 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 Landern · Feb 18, 2016 at 02:21 PM 0
Share

two things, why not just declare the variable in the for syntax, each frame that invokes the Update method would reset the i variable each invocation. Second, unless you know the size of the loop when deter$$anonymous$$ing things like distance or in the case angle it's a good idea to put a WaitForSeconds or yield out of the Update method so it's not iterating over the loop, you can introduce lag when a pause can occur from being in the loop. If the game had 100 Transform objects in that array and the game ran @ 60fps you would be looping over the array and deter$$anonymous$$ing the angle of 6000 transform objects(which may not change all that much within that second) times per second, chances are you can optimize this to say only run through the array 4 times a second(400 transform/angle considerations), or perhaps 1 time per second is enough...

avatar image Justice-V18 Landern · Feb 18, 2016 at 04:39 PM 0
Share

That is true, this code isn't supposed to be used for 100's of objects. Also thanks for pointing out i that can reset 0 to if declared before the for loop. The code bellow does what the script above does, but waits for x number of seconds before searching again:

 public Transform[] target;//add all objects here
 public float waitSec;

 float angle;

 void Start()
 {
     StartCoroutine (CheckDistance());
 }

 IEnumerator CheckDistance() 
 {
     int i;

     for (i = 0; i < target.Length; i++)//go from through whole array.
     {
         Vector3 targetDir = target [i].position - transform.position;
         Vector3 forward = transform.forward;

         angle = Vector3.Angle (targetDir, forward);

         print (target [i].name);

         if (angle < 5.0F) 
         {
             print ("Close to: " + target [i].name);
         }
     }

     yield return new WaitForSeconds (waitSec);
     StartCoroutine (CheckDistance());
 }

Also I'm not 100% sure if this is the best way to restart the Coroutine. But i did test it and it works.

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

43 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 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

Why does this loop throw an error if int questComplete rises above 1? 0 Answers

Managing In-Game Variables of Different Data Types 1 Answer

Array problem, I'm novice 1 Answer

Remove empty "" strings from array String[] 1 Answer

Cannot Set Array Size in Inspector 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