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
12
Question by vptb · Jul 25, 2014 at 11:47 AM · 2draycastcolliderlayers

Raycast ignore itself

I've worked with Raycasts for a long time now, and I understand how it works with layermasks, etc...

But I have a problem right now that I couldn't find in any question here.

Let's say I have a sentry gun on a 2D game, which is rotating over time searching for the player. Each frame it tests with a raycast if it sees the player. This raycast is ignoring everything except the layer "Obstacle" which is used on objects which the player can't go through (It's a tile based game). The objects with that layer are the level walls, but also some "traps", and the sentry gun is one of them.

So my problem right now is that I want to shoot a raycast from the sentry gun, but the raycast is hitting the sentry itself, and I can't create a layer for all the obstacle "traps" and shoot a raycast ignoring everything except that layer because that way the player wouldn't be able to hide himself from sentrys behind other kind of "traps".

So right now I'm searching for a way of having a raycast ignore everything except 1 layer, and also ignore 1 object inside that layer.

PS: I know that I can use RaycastAll and then ignore this collider, but I am looking for a more efficient way if it exists.

Comment
Add comment · Show 5
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 Paulo-Henrique025 · Jul 25, 2014 at 11:52 AM 0
Share

Can't you add another layer like "Enemy" and make the turret be part of this layer?

avatar image vptb · Jul 25, 2014 at 12:04 PM 0
Share

That will fall into the problem where it will then ignore all the "Enemy" objects, which I don't want, because the player can hide from this "Enemy" behind any other "Enemy". If this makes any sense...

avatar image Paulo-Henrique025 · Jul 25, 2014 at 12:12 PM 0
Share

Then another layer called "Stationary_Enemy", just for turrets and objects alike, you can have as many as you want.

avatar image vptb · Jul 25, 2014 at 12:18 PM 0
Share

Ok let's say that I have Turret A and Turret B, I create a "Sationary_Enemy" layer and add it to both Turrets. The player is currently behind the Turret B when Turret A is ready to fire.

When Turret A casts it raycast ignoring the layer "Stationary_Enemy" it will ignore both Turret A and Turret B, which in this case (since the player is behind Turret B) will make Turret A fire a projectile even though it doesn't see the player.

avatar image Paulo-Henrique025 · Jul 25, 2014 at 12:34 PM 0
Share

You can have a secondary collider to shield the player, or start the raycast outside the turret mesh.

5 Replies

· Add your reply
  • Sort: 
avatar image
60

Answer by Yword · Jan 06, 2015 at 10:46 AM

Unity 4.6.1 has the option turn on/off the ability to detect a collider that overlaps the start of any 2D line/raycast in Edit -> Project Settings -> Physcis 2D -> Raycasts Start In Colliders.

alt text


screenshot.png (85.9 kB)
Comment
Add comment · Show 4 · 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 Merwer · Dec 13, 2015 at 06:53 PM 1
Share

As many upvotes as I can give for you sir. I spent days trying to figure out why my Raycasts returned themselves 80% of the time. I looked through so many different forum answers and everyone kept saying to start the Raycast outside of my bounds.

This answer needs to be propagated more, especially since Raycasts seem to work different (by default) between 2D and 3D.

avatar image Fattie · Feb 09, 2016 at 11:54 PM -1
Share

yah it's a great tip! I sent "reward points"!

avatar image Nomenokes Fattie · Apr 01, 2016 at 09:46 PM -1
Share

Wow. I can't believe I didn't know that this entire time

avatar image Nomenokes Nomenokes · Apr 01, 2016 at 09:47 PM -1
Share

erm i didnt mean to reply to you fattie just in general. Sorry

avatar image
3

Answer by SilentSin · Jul 25, 2014 at 12:20 PM

I can see several possibilities:

  1. Does the turret have any colliders on its children that are on the wrong layer?

  2. Are you sure you're compiling the layer mask correctly? "~(1 << LayerMask.NameToLayer("Obstacle"))" would give you a mask of every layer except the obstacle layer.

  3. Are you sure its hitting the turret and not something else? Try using Debug.Log(message, object), giving it the object you hit as the second parameter so that when you single click the log message in the editor, it will highlight that object in the hierarchy.

  4. Are you sure you're casting the ray in the right spot? Try using Debug.DrawLine to draw the raycast in the scene view.

There isn't a way to have a raycast ignore a specific object unless you want to use RaycastAll (which you don't). There are valid use cases for RaycastAll, but this isn't one of them, the layer masking system should work.

Comment
Add comment · Show 9 · 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 vptb · Jul 25, 2014 at 12:29 PM 0
Share

How can the layer mask system work when I have two objects with the same layer, and I just want to ignore one of them without changing its layer? Please see the example I gave to Paulo Henrique.

Btw, I rewrote the question, I think it might have been misleading.

avatar image SilentSin · Jul 25, 2014 at 12:42 PM 1
Share

Your OP was a little unclear, the example clears it up a bit.

The layer mark system should still work as long as you are casting a ray from inside the collider of your turret, in which case the ray won't hit that collider, but it will still hit the other turret in front of the player.

If you have addressed all 4 things I suggested in my previous post, maybe you could post a screenshot of the scene view with the debug line in it so I can see where everything is located. Preferably with all of the objects in the scene selected so that I can see all the colliders.

avatar image vptb · Jul 25, 2014 at 01:01 PM 0
Share

1- The turret doesn't have any child object;

2- I'm casting the ray hitting only that layer, so at the moment I'm using this:

 Physics2D.Raycast(transform.position, dir, dist, Layer$$anonymous$$ask.Get$$anonymous$$ask("Obstacle"));

3- Yes I made sure using the Debug.Log(message, object);

4- This was one of the first things I tested.

As I mentioned above I'm using the Raycast from Physics2D, and I didn't know it would ignore its own collider.

avatar image SilentSin · Jul 25, 2014 at 01:11 PM 0
Share

I didn't realise that you'd be using 2D physics. I would be using 2D for my current game if it had capsule colliders :(

I don't know if raycasting from inside a 2D collider ignores it like it would in 3D. It should be simple enough to test though. If that turns out to be your problem, you could just move the origin of the ray forwards a bit. So ins$$anonymous$$d of calling ...Raycast(transform.position, dir... you'd call ...Raycast(transform.position + dir, dir... or you could multiply dir by 0.5 or something if 1m might be too far forward.

avatar image Vitor_r · Jul 25, 2014 at 04:46 PM 1
Share

What about disabling the collider of the turret when the ray starts and when it hits something you enable again? $$anonymous$$ind of hackish but could work.

Show more comments
avatar image
3

Answer by Amit_B · Oct 25, 2014 at 05:45 PM

I had the same problem, this is how I solved it:

Physics2D.Raycast(rayCasterPosition + directionOfRayCast*this.collider2D.bounds.size.magnitude, directionOfRayCast, distance+1);

directionOfRayCast is normalized.

To put it more simply, I shot the ray outside of my own 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
avatar image
2

Answer by Pangamini · Mar 19, 2018 at 03:20 PM

Whenever I need to do some custom raycast collider filtering, I use RaycastNonAlloc (works for both 2D and 3D physics). It's actually RaycastAll but allows you to use pre-allocated array for results (for performance reasons).

The way how it works is that it returns all potential hits, and lets you select the one you like. The simplest usage would look like this: Collect all hits, sort them by distance. Go through them in the closest-to-farthest order, stop at the one that matches your criteria.


Don't be worried about the extra cost of collecting all hits. The physics engine has to do very similar thing when doing a regular Raycast anyway.

You can use this method to filter out collider hits based on anything where layers won't help, eg. texture alpha on the hit position (that's what I've been using it for, mostly)

Some extra tips:

  • Consider filtering out collisions before sorting (see what's more expensive, filtering or sorting in your use cases)

  • When sorting, don't even think about doing vector.magnitude! all you need is sqrMagnitude when comparing multiple vector lengths (as sqrt is an one-to-one function), you will save a lot of sqrt's which are quite expensive.

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

Answer by Tsilliev · Mar 19, 2018 at 12:28 PM

My solution was to turn off the collider before the raycast and turn it on at the end:

 for (int i = 0; i < sectorCount; i++) 
 {
 locations [i].GetComponent<CircleCollider2D>().enabled = false;
                 
 RaycastHit2D hit = Physics2D.Raycast (locations [i].transform.position, Vector2.up);
 .
 .
 stuff
 .
 .
 locations [i].GetComponent<CircleCollider2D>().enabled = true;
 }
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 Armend · Apr 28, 2019 at 06:31 PM 0
Share

It's an interesting way to achieve it, but you have to consider the fact, that if your character is standing in an "isTrigger"-Collider from another object, that OnTriggerExit and OnTriggerEnter will be called every frame because of the removed and then added collider of your player.

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

32 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

Related Questions

Detecting if the object is hit by LineRenderer 1 Answer

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

3d raycasting in 2d world with perspective camera 0 Answers

Make Certain Object's Raycasts Ignore Certain Colliders 0 Answers

Raycast2D Layermasks and incomplete docs 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