• 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
Question by guru20 · Aug 08, 2013 at 02:29 AM · shaderraycastraycastingrendereralpha

How to set alpha for object "hidden" by another?

I have a game in which the camera is in front of, and always facing the object and moving along with the object (similar to a 2D scroller, but in this case it is 3/4-down aerial 3D view)... since there are (a few) areas of the level where the player could pass behind another object and be hidden from sight, I'd like to render the player mesh at 50% alpha.

What is the best way to do this? I have searched Answers and Forums and found bits and pieces, but still having problems. My scenario is this:

1) I have 2 cameras for rendering different layers; one uses culling to render the level/background, the other renders the player object (so, in fact, right now when if you fall "behind" something, you are actually rendered in front of it -- even more confusing!)

2) The player object includes multiple children/grandchildren (including a multi-mesh model and a point light); these meshes all have Transparent diffuse shaders attached to allow setting alpha levels

I am attempting to do a Raycast to check whether something is in front of (hiding) the player, and set alpha accordingly, but it's not working...

 function Update () {
     var cam : Transform = Camera.main.transform;
     var ray = new Ray(cam.position, cam.forward);
     var hit : RaycastHit;
     if(Physics.Raycast (cam.position, cam.forward, hit, 100)) {
         if (hit.collider != currentPlayer.collider) currentPlayer.renderer.material.color.a = 0.5; // if player not visible, change alpha
     }
     else currentPlayer.renderer.material.color.a = 1.0;
 }
 

For one thing... I assume doing this doesn't work on the player object's children -- what is the best way to call a child or set of children (I could tag them if that helps); I don't need to render the whole game object, but the main model mesh which is a child of the collider object.

Comment

People who like this

0 Show 4
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 mouurusai · Aug 08, 2013 at 02:53 AM 0
Share

I'm not pretty sure, but i think, you may do something like you want, with combination of DepthMask and Stencil buffer.

avatar image guru20 · Aug 08, 2013 at 03:19 AM 0
Share

Hmmm... maybe. I will have to read up on this. At first glance, the examples seem to be doing the opposite of what I want ie. NOT rendering something when it is actually in front; whereas I want TO render something, when it is actually behind ... but moreso, I want to render it differently, with a 50% see-through to give an "x-ray" effect through the blocking mesh and onto the player mesh; even though in actually I am rendering the player in front of the obstruction, it would still have this effect... But I will need to research more about DepthMask and/or Stencil. They are more complex than any of the rendering I have done so far...

avatar image guru20 · Aug 08, 2013 at 04:42 AM 0
Share

okay, after some more web searching I have found this, which may prove useful: http://chrisrolfs.com/game-dev/unity3d/hide-objects-blocking-player-view

avatar image guru20 · Aug 08, 2013 at 04:44 AM 0
Share

And also this, which may just do the trick for me (I use the Unify wiki but guess didn't look in the right place for this solution -- didn't think it would be a special shader to do it. Man, I really need to learn more about customizing shaders...)

0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Cutout Shader getting Opaque when turning alpha off and on again 1 Answer

I want to paint the surface of a mesh depending on if it has been seen by a camera, can this be done with shaders? 0 Answers

fading alpha renderer.a vs shader performance 1 Answer

Shader Graph Increment vector1 1 Answer

Turret that shots a raycast to detect my character then shots at him. 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