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 /
avatar image
0
Question by SirPleb · May 03, 2020 at 05:47 AM · raycastloopraycasthitrayforeach

Raycasting and foreach loops

I'm currently trying to write a piece of code that does something that is theoretically very simple. I have a list of walk points and scientists that I need to test if an alien (the game object where this script is attached) can see. I'm trying to use raycasting to determine this. here's the code I have so far:

 public List<GameObject> test;

 public List<GameObject> importantThingsToSee;
 public List<GameObject> seeableScientists;
 public List<GameObject> seeableWalkPoints;

 public void Awake()
 {
     importantThingsToSee.AddRange(GameObject.FindGameObjectsWithTag("Scientist"));
     importantThingsToSee.AddRange(GameObject.FindGameObjectsWithTag("Walk Point"));
     PickANewPoint();
 }

 public void PickANewPoint()
 {
     seeableScientists.Clear();
     seeableWalkPoints.Clear();
     foreach (GameObject importantGameObject in importantThingsToSee)
     {
         Ray ray = new Ray(transform.position, transform.position - importantGameObject.transform.position);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit))
         {
             test.Add(hit.transform.gameObject);
             foreach (GameObject compareImportantGameObject in importantThingsToSee)
             {
                 Debug.Log(hit.transform.gameObject + " when compared to " + compareImportantGameObject);
                 if (GameObject.ReferenceEquals(hit.transform.gameObject, compareImportantGameObject) && compareImportantGameObject.tag == "Scientist")
                 {
                     Debug.Log("... is a successful scientist!");
                     seeableScientists.Add(compareImportantGameObject);
                 }
                 else if (GameObject.ReferenceEquals(hit.transform.gameObject, compareImportantGameObject) && compareImportantGameObject.tag == "Walk Point")
                 {
                     Debug.Log("... is a successful walk point!");
                     seeableWalkPoints.Add(compareImportantGameObject);
                 }
                 else
                 {
                     Debug.Log("... is a faliure.");
                 }
             }
         }
     }

 }


The code should work like this: I have one list of both the walk points and the scientists. When I run the game, that list gets filled correctly. Then it makes a ray and casts it to every gameobject in that list. At this point, I tried to compare the hit to the game object it was referencing at the time, but the raycast was out of sync with the foreach loop by the time that if statement was called. My "solution" was to put another foreach loop inside of the original and test the hit with every single game object in the importantThingsToSee list. In my scene there should be 6 objects it does a raycast for, but it's only doing 5, and at least 2 of those are going in what appears to be the exact opposite direction of where they're supposed to be going. Any help would be appreciated, thanks!

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
0
Best Answer

Answer by Bunny83 · May 03, 2020 at 10:21 AM

Uhm you know that vector subtraction create a vector in the form "tip - tail". So your raycast direction goes from the object you want to test towards your alien. However you start the ray at your alien object and therefore in the wrong direction.


I don't really understand your whole "out of sync" issue you're describing. I don't see any reason for your nested loop.


I would suggest something like that:

 public void PickANewPoint()
 {
     seeableScientists.Clear();
     seeableWalkPoints.Clear();
     foreach (GameObject importantGameObject in importantThingsToSee)
     {
         Vector3 dir = importantGameObject.transform.position - transform.position;
         Ray ray = new Ray(transform.position, dir);
         RaycastHit hit;
         if (!Physics.Raycast(ray, out hit))
             continue; // we hit nothing? check next object
         test.Add(hit.transform.gameObject);
         if (hit.transform.gameObject != compareImportantGameObject)
             continue; // the object we hit is not the object we want to test? check next object
         if (compareImportantGameObject.CompareTag("Scientist"))
         {
             Debug.Log("... is a successful scientist!");
             seeableScientists.Add(compareImportantGameObject);
         }
         else if (compareImportantGameObject.CompareTag("Walk Point"))
         {
             Debug.Log("... is a successful walk point!");
             seeableWalkPoints.Add(compareImportantGameObject);
         }
         else
         {
             Debug.Log("... is a faliure.");
         }
     }
 }

Note that the use of "continue" reduces the nesting depth and makes a clearer cut. Note that the reason you don't need the inner loop is that we iterate through all objects anyways. So if one object is hidden behind another it can not be seen since the raycast will always stop at the first object. So when you raycast against the object further away you will hit the closer one which of course is the wrong object. However when you later check the closer one you will detect the closer object properly. It should be obvious that you will never hit an object behind some other object.


If your / this approach is the right one depends on how you specify "can be seen". Since you just use a raycast against the objects pivots you essentially state that only when you have a clear sight along that line to the object, the object can be seen. If that's not what you intended you need a different approach. If you only want to make level geometry to be able to block the sight but not other object that you want to detect, you need a different approach.


A common solution for this is to first make sure all your relevant objects are on a seperate layer(s). More specifically all geometry that should be able to block the sight is on a layer. Instead of a Raycast you would simply use a Linecast between your alien and the object you want to test. However you would only cast agains the blocking geometry, not the actual objects you're interested in. If the line cast doesn't detect anything you have a clear sight to that object. If it does detect something it has to be blocking geometry and the sight is blocked.

Comment
Add comment · Show 1 · 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 SirPleb · May 05, 2020 at 07:48 PM 0
Share

Yep, that worked perfectly. I didn't realize I had my ray going backwards. The scene I had set up for this had the objects in a plus sign, so when the objects were getting added in the wrong order, I thought that the ray was lagging behind or something. Turns out it was just going backwards. Thank you!

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

174 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 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 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 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 avatar image avatar image

Related Questions

How can I can I cast a ray from a gameobject? 1 Answer

Fall collision force with help of raycast 0 Answers

RayCast2d Not colliding with objects in other layers 1 Answer

Raycast won't hit Collider 1 Answer

Not getting right value; ScreenPointToRay 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