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 douglasg14b · Apr 17, 2015 at 07:24 PM · raycastlayers

How to raycast through friendly objects?

I have turrets on ships as seen here: alt text

These turrets will rotate towards a target, but will not shoot at it till the turret is directly pointing at the enemy ship (any spot on the enemy ships collider). I do this so that turrets will not try and aim for the middle of an enemy before firing, and so that they only fire when facing the enemy. I accomplish this with a raycast. However, since all units in the scene (including enemy units) are on the same layer, I cannot ignore the Unit layer when doing the raycast (this is not changeable due to pathfinding). This causes the raycast to be blocked by allied units and they will not fire at the enemy (I still want them to shoot, even if the enemy is not within line of sight).

How would I go about doing this? RayCastAll seems to be a bit expensive performance wise if I was to have hundreds of units using it per frame, especially if there are a large number of units between the ones firing and the enemy.

Comment
Add comment · Show 3
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 Glurth · Apr 17, 2015 at 07:53 PM 0
Share

Since you have them all on the same layer, you could loop through the enemies and use each one's collider to test collider.raycast to detect a hit on each. http://docs.unity3d.com/ScriptReference/Collider.Raycast.html

avatar image douglasg14b · Apr 17, 2015 at 08:57 PM 0
Share

Ah, that's a pretty good solution. However that brings up another problem, how to make it hit some objects like buildings or environmental objects like asteroids...etc

avatar image Glurth · Apr 17, 2015 at 11:10 PM 0
Share

You can add any gameobject with a collider to the list you check.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · Apr 17, 2015 at 10:24 PM

you could add an empty game object as a child and add a collider to it and place that in another layer allowing you to basically have multiple layers on a single object as long as you make sure you get the transform.parent.

also the best solution is a small modification to your pathfinding code.

Layers for casting against can be AND/OR together

for example you can cast against layer 1 and 2 but as a single layer as it were.

what you should have is a allied layer and an enemy layer and in pathfinding as the layer do it as allied/enemy.

anytime you have a layer input you can use a a legal input the input

int | int

so

physics.raycast(...., layer 1 | layer 2) will work on either layer

alternatly

int Layer = 1<<4 | 1<<5;

is a a Layer now that works against either layer 4 or 5

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 douglasg14b · Apr 17, 2015 at 11:16 PM 0
Share

Thanks for the reply. The pathfinding is based on Unit/Static Object/Ground. "enemy" is arbitrary since this will be a multiplayer game, ins$$anonymous$$d it will have Player1, Player2...etc

I could have a layer for each player, so that I can cast just against that layer. Ie Player 2,3 and 4 are enemies, I would cast against their respective layers.

As for the actual layer mask part of it. I found making a public field of type Layer$$anonymous$$ask, and assigning the layers in the inspector was much easier.

Having a child object with the other layer sounds like a good idea, I just need to make sure I'm not putting undue stress on the performance of the game with mesh colliders.

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

20 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

Related Questions

Raycast Layermask doesn't ignore no layered Parent? 1 Answer

How do i add a layer to a gameobject? 1 Answer

Make Certain Object's Raycasts Ignore Certain Colliders 0 Answers

Shoot through walls 1 Answer

Ignore clicks on all layers except one 0 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