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 /
avatar image
0
Question by xthunderduckx · May 17, 2019 at 02:36 PM · physicsraycastlayers

Stop raycast from hitting same gameObject twice

I've created a script now that can detect all enemies in a radius. In order to stop enemies who are behind walls from being harmed by the explosion radius, a raycast is sent out in order to ensure that it hits an enemy and not a wall. Unfortunately, if there are two enemies in the same direction, then two raycasts are going to be sent towards the same enemy, thus hitting them multiple times.


My question is: How can I stop an enemy standing in front of another enemy from being hit by a raycast that is intended to hit the object behind it?

 if(collision.gameObject.tag == "Enemy") { collision.gameObject.SendMessage("takeDamage", 125); }
         grenadeBody.velocity = Vector3.zero; 

         Collider[] collisionsInRadius = Physics.OverlapSphere(transform.position, 3f); //Needs layermask so it only detects gameobjects with enemy tag.  
         for(int i = 0; i < collisionsInRadius.Length; i++)
         {
             Ray explosionRay = new Ray(transform.position, collisionsInRadius[i].transform.position - transform.position);
             if (Physics.Raycast(explosionRay, out RaycastHit explosionHit, 3f))
             {
                 if(explosionHit.collider.gameObject.tag == "Enemy")
                 {
                     collisionsInRadius[i].gameObject.SendMessage("takeDamage", 225);
                 }
             }
         }
         //play explosion effect.  
         Destroy(this.gameObject);
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Zaeran · May 17, 2019 at 04:23 PM

You'd likely have to do a Physics.RaycastAll, and then order the hit colliders by distance. Iterate through from closest to furthest, and stop when you hit a wall.

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 xthunderduckx · May 17, 2019 at 05:30 PM 0
Share

So does Physics.RaycastAll send out a ray and the ray continues regardless of the number of times it hits an object?

if that's true, I could sort them by distance, and then apply damage to all gameObjects with tag enemy, and if the distance of a wall is closer than the distance of one of the enemies, then ignore whatever is past it right?

Thanks.

avatar image xthunderduckx · May 20, 2019 at 02:45 PM 0
Share

Actually, never$$anonymous$$d. This doesn't actually answer my question. $$anonymous$$y script already did that as I realize now, except it did raycast all. This still doesn't solve my issue with an enemy having the potential to be hit multiple times.

avatar image
0

Answer by xxmariofer · May 20, 2019 at 03:35 PM

just create a list on Colliders

 List<Collider> colliders = new List<Collider>();

change raycast for raycastall

 RaycastHit[] hits;
 hits = Physics.RaycastAll(transform.position, collisionsInRadius[i].transform.position - transform.position, 100.0F);

 foreach(RaycastHit hit in hits) {  
    if(hit.collider.gameObject.tag == "Enemy" && !colliders.Contains(hit.collider))  { collisionsInRadius[i].gameObject.SendMessage("takeDamage", 225); colliders.Add(hit.collider); }
 }
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

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

When i bring my mouse on layer mask my player doesn't move 0 Answers

Some raycasts don't hit the object 0 Answers

Colliders attached to objects on separate layers are colliding? 1 Answer

Raycast ignoring layers that it should not 2 Answers

How do I use layermasks? 9 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