Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Bewat · Apr 27, 2020 at 08:40 AM · linecastfor loop

Multiple raycast in for loop uses same RaycastHit

Hi! I'm quite new to coding and I've been trying to make a "sight" script for my characters, but something's not working and I'm not able to figure out what.
The script works by first taking all objects within viewing distance (using a sphere collider) and then from those getting the ones within a certain angle (Field of View). This works fine, but then when I try to see if the objects that are within the field of view is obscured or not, something's off. It works for one object, but when multiple objects are in view the character sometimes think that only one of them is in plain sight.

The plan was to cast a Linecast between the character and the object and see what it hits. If it hits the same object that its aiming for nothing is obscuring, but if not something is in the way. From what I could gather it seems that the script sometimes uses the same hitinfo for different Linecasts. I don't know if it is something i don't know about the for-loop. I appreciate all help. This is my script:

     void OnTriggerEnter(Collider col)
     {
         if (col.CompareTag("Creature")) { objectsInDistList.Add(col.gameObject); }
     }
     void OnTriggerExit(Collider col)
     {
         if (col.CompareTag("Creature")) { objectsInDistList.Remove(col.gameObject); }
     }

     //This is called in update
     void GetCreaturesInView()
     {
         for (int i = 0; i < objectsInFOVList.Count; ++i)  { if (!objectsInDistList.Contains(objectsInFOVList[i])) { objectsInFOVList.Remove(objectsInFOVList[i]); } }
         for (int i = 0; i < objectsInViewList.Count; ++i) { if (!objectsInFOVList.Contains(objectsInViewList[i])) { objectsInViewList.Remove(objectsInViewList[i]); } }

         //see which objects are in the field of view
         foreach (GameObject obj in objectsInDistList)
         {
             Vector3 objDirection = obj.transform.position - transform.position;
             float angle = Vector3.Angle(objDirection, transform.forward);
             if (angle < FOV)
             {
                 if (objectsInFOVList.Contains(obj)) { }
                 else { objectsInFOVList.Add(obj); }
             }
             else { objectsInFOVList.Remove(obj); }
 
         }

         //See if object is obscured or not
         //this is where the problem begins I think
         for (int i = 0; i < objectsInFOVList.Count; ++i)
         {
             GameObject obj = objectsInFOVList[i];
 
             if (Physics.Linecast(this.transform.position, obj.transform.position, out RaycastHit hit))
             {
                 if (hit.transform.gameObject == obj)
                 {
                     Debug.Log("True Hit: " + hit.transform.name + ", " + "Obj: " + obj.transform.name);
                     Debug.DrawLine(transform.position, obj.transform.position, Color.green);
                     if (!objectsInViewList.Contains(obj)) { objectsInViewList.Add(obj); }
                 }
                 else
                 {
                    //this often runs for the second object in view where the raycasthit is the same as the first.
                     Debug.Log("False Hit: " + hit.transform.name + ", " + "Obj: " + obj.transform.name);
                     Debug.DrawLine(transform.position, obj.transform.position, Color.red);
                     objectsInViewList.Remove(obj);
                 }
             }
         }
     }


Sorry for the long code but I'm not sure what is important for this problem.

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

0 Replies

· Add your reply
  • Sort: 

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

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

Enemy doesn't flip properly with linecast 0 Answers

3 for loops and a IF crashes when the if is true 1 Answer

Raycast2D 360 Rotation 0 Answers

Destroy gameobjects in an array using a for loop. 0 Answers

How can I check a Linecast across multiple layers, and assign the result to a single bool? 1 Answer


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