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
1
Question by mradfo21 · Oct 21, 2013 at 07:59 PM · shaderraycasttransparencyselectionclicking

raycast with transparent shader

Hello there unity community! This will be my first question and I fear it is one without a good answer.

I'm creating a game with a slightly isometric perspective, but featuring 3d objects with interiors. You issue orders to your soldiers by clicking the map, they create a path and head towards it (using navmesh and agents)

Now I have this problem.. you need to be able to issue orders to move inside the buildings, between floords.. but.. there are roofs.

So to tackle this is I wrote a cutaway shader that I can control. This works beautifully. However it presents another problem. To actually tell the agents where in 3d space to move, i raycast from the camera and get a position of first collision. However, because this transparency is being created in a shader, this raycast still sees the object as if it has a roof (which makes sense..)

My question to the brilliant minds of the unity community:

Is there an elegant solution for dealing with raycasting through transparent objects?

alt text

I come from a visual effects background so.. i've got some ideas of generating a world position pass, then figuring out the pixel color under the mouse, then using this to generate the actual location in 3d space.. somehow all offscreen..

but i know this issue has come up before in game development. I think about all the times that i've fired a rife through a fence (that is made with a transparent texture) when the texture is present, the bullet collides with the fence, when firing through the gaps, it strikes the character behind. How are they doing this?

transparentshader.gif (48.7 kB)
Comment
Add comment · Show 1
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 Kecske85 · Mar 27, 2014 at 08:26 PM 0
Share

Could you please share the shader?

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by whydoidoit · Oct 21, 2013 at 08:06 PM

So try this:

  1. Flag the floor/ceiling objects with a tag of "Floor"

  2. Use RaycastAll to get a list of all of the hits

  3. Work through that list ignoring floor tagged objects that are above your cutaway position and take the first non-cutaway object as being your hit.

      using System.Linq;
         ...
         bool Cast(Vector3 position, Vector3 direction, float distance, out RaycastHit result, LayerMask mask, float cutOffPoint)
         {
         
            var hits = Physics.RaycastAll(position, direction, distance, mask).OrderBy(h=>h.distance);
            foreach(var hit in hits)
            {
                if(!hit.collider.CompareTag("Floor") || hit.point.y < cutOffPoint)
                {
                      result = hit;
                      return true;
                }
            }
            return false;
         
         }
    
    
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 mradfo21 · Oct 21, 2013 at 08:09 PM 0
Share

thats a very good idea!

avatar image whydoidoit · Oct 22, 2013 at 10:44 AM 0
Share

If it works for you, could you tick the answer?

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

16 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

Related Questions

How do I restore a shader after a raycast? 1 Answer

Add Color Property to Unlit Alpha? 1 Answer

Wrong weighted order transparencry 0 Answers

Transparency and texture atlases, shader calls affecting performance? 2 Answers

Shader for 2D Sprite "Always Rendered on Top" Problem 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