• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

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

My LayerMask is not working 1 Answer

Layermask (raycast) wont work... 3 Answers

Using unity 4.6 UI and raycast2D functionality 0 Answers

Why is Physics.Raycast returning colliders in a layer not included in the layermask? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges