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 TRON-dll · May 05, 2014 at 09:34 PM · raycast3d

How can I affect an object, but only while it's being hit by a raycast?

Hello!

What I'm trying to do is make it so that certain objects in my scene will turn transparent while they are obstructing the camera's view of the player. My current method is as follows:

  • Raycast from the camera's position and in the camera's orientation

  • If the object the ray hits is tagged with a specific tag, switch that object over to a transparent material.

While this works for turning those objects transparent when I need them to be, I'm not sure what I should do to switch them back to their non-transparent material. Any tips?

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
Best Answer

Answer by Clet_ · May 06, 2014 at 12:15 AM

One way to do it would be to switch a boolean every frame, depending on its transparency.

 using UnityEngine;
 using System.Collections;
 
 public class MyClassThatNeedsToGoTransparent : MonoBehaviour {

     public bool isObstructingPlayer = false;
     private bool isTransparent = false;
 
     private void LateUpdate () {
         if(!isTransparent && isObstructingPlayer) {
                 //Do stuff to make it go transparent

                 isTransparent = true;
         } else if(isTransparent && !isObstructingPlayer) {
             //Do stuff to turn it back to normal

                  isTransparent = false;
         }
 
         isObstructingPlayer = false;
     }
 }
 
 [RequireComponent(typeof(Camera))]
 public class MyCameraThatChecksForObstructingObject : MonoBehaviour {

     private Camera myCamera;

     private void Awake () {
         myCamera = camera;
     }
     private void Update () {
         RaycastHit info;
         if(Physics.Raycast (myCamera.ScreenPointToRay (transform.position), out info)) {
             if(info.transform.tag == "TheWantedTag") {
                 info.transform.GetComponent<MyClassThatNeedsToGoTransparent>().isObstructingPlayer = true;
             }
         }
     }
 }

The first class needs to be attached to the gameObject's you want to go transparent, and the second one to the camera that does the check

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 TRON-dll · Jun 06, 2014 at 05:44 AM 0
Share

That worked perfectly! Now I guess I know how to reference other scripts, thanks!

avatar image
0

Answer by Jeff-Kesselman · May 06, 2014 at 12:44 AM

The simplest thing would be to save the material for the object in a hash table and then set it back when it has to turn opaque.

Another approach would be to always use a transparent material and just tint its opaqueness with the color setting on the shader.

I wouldn't recommend the latter though only because transparent shaders generally have sort issues and arent ideal for everyday usage.

Edit: Oh, , or are you asking how to determine when the ray isn't hitting the object?

I do something something like this in my world builder. What I do is keep a list of all the objects I have set transparent. When the view angle moves, I set them all opaque and clear the lat, then generate a new one. ONLY do it when the camera has moved though, or its going to get pretty expensive.

In my case, my world is actually made up of cubic areas and I change entire cubes at a time which cuts significantly down on the processing.

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.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Using RayCast to interact with door ? 0 Answers

RaycastHit always returns 0 1 Answer

FPS shooting 1 Answer

How do i get the Raycast rotation? 1 Answer

nav mesh agent stop ScreenPointToRay after a while 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