Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

RayCast help!

Discussion in 'Scripting' started by jakebaker865, Jun 1, 2020.

  1. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36
    Hi All,

    I have a script where upon Raycast hit, the floor above my player fades. It works, but it's causing glitching with my player. It also has the issue of only hiding the object directly in front of the player, whereas I would just like everything on a higher plane than my character to fade or disappear. Would anyone like to help make this work better?

    Code (CSharp):
    1.         public float DistanceToPlayer = 5.0f;
    2.         public Material TransparentMaterial = null;
    3.         public float FadeInTimeout = 0.6f;
    4.         public float FadeOutTimeout = 0.2f;
    5.         public float TargetTransparency = 0.3f;
    6.         private void Update()
    7.         {
    8.             RaycastHit[] hits; // you can also use CapsuleCastAll()
    9.                                // TODO: setup your layermask it improve performance and filter your hits.
    10.             hits = Physics.RaycastAll(transform.position, transform.forward, DistanceToPlayer);
    11.             foreach (RaycastHit hit in hits)
    12.             {
    13.                 Renderer R = hit.collider.GetComponent<Renderer>();
    14.                 if (R == null)
    15.                 {
    16.                     continue;
    17.                 }
    18.                 // no renderer attached? go to next hit
    19.                 // TODO: maybe implement here a check for GOs that should not be affected like the player
    20.                 AutoTransparent AT = R.GetComponent<AutoTransparent>();
    21.                 if (AT == null) // if no script is attached, attach one
    22.                 {
    23.                     AT = R.gameObject.AddComponent<AutoTransparent>();
    24.                     AT.TransparentMaterial = TransparentMaterial;
    25.                     AT.FadeInTimeout = FadeInTimeout;
    26.                     AT.FadeOutTimeout = FadeOutTimeout;
    27.                     AT.TargetTransparency = TargetTransparency;
    28.                 }
    29.                 AT.BeTransparent(); // get called every frame to reset the falloff
    30.             }
    31.         }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    No idea how your game is set up, but one idea is to set up trigger volumes on each floor that hides all floors above it, and re-enables them when you leave the lower floor, either going up (reveals floor) or going down (hides floor you were just on).
     
  3. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36
    Thanks for the advice. I thought maybe trying that bit got lost in how to set it up. My floors aren't grouped separately, so I cant just say for the gameobject to be turned off when enter/exiting collider. Any way you can think to turn off floors marked by their "layer"? (I.e. set up a "top floor" layer and when entering collider fading all mesh renders marked "top floor"?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    You could write a script that iterates all the objects in your scene after they load, and then actually puts them into lists for Floor1, Floor2, etc. based on the position of their bounding boxes or their Transform.position. You probably would limit it to just geometry (things with Renderers), Lights, Particle Systems, etc., things that affect visuals.

    Taking this "late find" approach means you can put the level together ad-hoc and then at runtime it would make lists of what is in what layer, and you could turn all of them off except the one you're on, or perhaps the one you're on and the one below it.

    You could also write an Editor script to do this in the Editor and dynamically reorganize your level into slices, then just operate on the slices.
     
  5. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36
    This sounds like a great way to go. I would need to learn a crapton more for this, though. I should have mentioned I'm a terrible scripter newbie (going on about 3 months).

    If I were to go this route, what terms would you search for in order to learn how to script this?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Here's some starting points:

    Use GameObject.FindObjectsOfType<Transform>() to find all transforms in the scene. OR... use GetComponentsInChildren<Transform>() if you know which "top level" GameObject all your scene is under.

    Using Debug.Log() you can print out how many you found, just as a sanity check (using the array .Length property).

    From each entry in that array you can check if it has a Renderer on it with GetComponent<Renderer>();

    If it has a renderer, you'll get back a non-null reference, and from that reference you can check the bounds property, or else just go back to the original transform and use its position.

    From the Y-axis of its position you can decided which level it is on. I.e, if your floors are 4 units high, you can divide by 4 and get an idea of the floor.

    From this floor number you could put the objects into separate lists, or else some kind of Dictionary, or just print their names out with Debug.Log() to make sure you're on the right track.

    Obviously if an object is positioned on one level but extends through multiple level, the position will only be on one level. BUT... the Bounds volume might tell you that it is a very tall object and you have to decide what to do with that one.

    Also, regardless if you end up going this way, it is an easy stand-alone script that you can make and play with and learn a bit, and if you don't end up like that approach, it doesn't have to have changed your game at all, except for adding an instance of the script.

    Take it one step at a time, check the docs, come back frequently if you have specific questions, and prepare to have your brain expanded! It's a glorious feeling. :)
     
  7. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36
    Thanks. I'll see what I can do, and if I have any questions, I'll be back. Not on this thread, but you get what I mean.
     
    Kurt-Dekker likes this.