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 dustingunn · Mar 01, 2012 at 06:40 AM · raycastlayerslayermask

RayCast vision testing for same layer objects?

I have multiple enemy objects with multiple hitboxes around the limbs, all in the same layer. The problem with testing their vision to the player or other enemies is that the raycast will collide with its own hitbox, or if layermasked, ignore other enemies.

This is the solution I tried:

     var hits : RaycastHit[];
 sightPoint.LookAt(trgt);
 //var trgtDir : Vector3 = Vector3.Normalize(trgt.position - transform.position);
 transY = transform.position.y;
 
 hits = Physics.RaycastAll (sightPoint.position, sightPoint.forward, 20, layerMask);
 //test all hits in line, test obstacle distance against target distance
 var wallDist : float = 100;
 var tarDist : float = 0;
 for (i=0; i<hits.length;i++) 
 {
     //if hits wall, set wallDist
     if (hits[i].transform.root != trgt && hits[i].transform.root != this.gameObject
         && hits[i].distance < wallDist) {
         wallDist = hits[i].distance;
     }
     //if hits target, set tarDist
     if (hits[i].transform.root == trgt) {
         tarDist = hits[i].distance;
     }
 }
 if (tarDist != 0 && tarDist < wallDist ) {
     lastSeenPos = trgt.position;
     return true; }
 else {
     return false; }

It seems very unreliable and randomly fails even when the target is clearly in sight. There has to be a better way.

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
0

Answer by Tommynator · Mar 01, 2012 at 01:02 PM

Is it really necessary to use RaycastAll? If not, try using the Raycast function that you get within the Collider class. So you could go through all you Head Hitbox colliders of your enemies and run the collider.Raycast() function - it's ignoring collisions with the calling collider by default.

An alternative (not very nice) solution is to set the layer of the enemy you are casting from temporarily to IgnoreRaycast. After the raycast you can set it back to normal. Not sexy but should work.

Btw - I didn't understand what you are trying to do with the wallDist checking. Is it to avoid false positives - e.g. the target is behind 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 dustingunn · Mar 01, 2012 at 08:21 PM 0
Share

Yeah, the hits are out of order so I could only think to compare the lowest distance of the wall to the distance of the target

avatar image dustingunn · Mar 03, 2012 at 12:23 AM 0
Share

It says Collider.Raycast ignores all colliders except itself, which is kind of the opposite of what I need.

avatar image
0

Answer by Owen-Reynolds · Mar 01, 2012 at 03:26 PM

No matter what method you use, add one more check for if you miss everything (in your case, hits is empty.) It sounds odd, but if a ray says there's nothing between me and you, I can see you.

What happens, as the math and colliders get stranger, is you raycast through an armpit, or one of you is leaning back, so your 20 meter ray stops just in front of their face (and increasing the raycast distance makes "through the armpit" hit the wall behind them.)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Usage of Layer masks, what am I doing wrong? 1 Answer

Help with Layermasks 1 Answer

How do I use layermasks? 9 Answers

Layer mask doesn't work... 1 Answer

Why is Raycast ignoring the layer mask? 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