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

Discussion LineRenderer works fine (it's just me being silly)

Discussion in 'Editor & General Support' started by Techmologue, Jan 24, 2023.

  1. Techmologue

    Techmologue

    Joined:
    Nov 21, 2017
    Posts:
    41
    Solved! I have made a classic mistake

    Previously:

    Hello Friends.
    Currently messing around with implementing lasers and I am not quite sure but my line renderer component hits everything even objects on layers that are disabled for the line renderer
    I am even limiting the raycast code to exclude a certain Layer... but that has no effect at all. The laser still stops at the excluded layer object.;

    Can someone point me into the right direction, on what I might be missing?

    Thank you
    Mike

    Unity 2022.2.2f1 Core 3D
     
    Last edited: Jan 26, 2023
  2. Techmologue

    Techmologue

    Joined:
    Nov 21, 2017
    Posts:
    41
    Can the LineRenderer even be configured to ignore objects that have a certain layer mask assigned?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Let's get the terminology straight here. LineRenderer doesn't "hit" anything.

    Are you perhaps speaking of Raycasting? If so, that can be configured via LayerMask in order to decide what Layers it might consider hitting, plus there are MANY different raycasting processes.

    Here's more random notes:

    Raycasting, colliders, planes, Plane, etc:

    https://forum.unity.com/threads/hel...s-are-hitting-an-object.1058393/#post-6840296

    https://forum.unity.com/threads/kee...within-the-view-frustrum.936209/#post-6117173

    And there is also EventSystem.RaycastAll for raycasting in the UI / EventSystems context.

    Raycasting in 2D:

    https://forum.unity.com/threads/ran...ear-polygon-collider-2d.1060082/#post-6851372

    The difference between Layers vs LayerMasks:

    https://forum.unity.com/threads/raycast-layermask-parameter.944194/#post-6161542

    Always use named arguments with
    Physics.Raycast()
    because it contains many poorly-designed overloads:

    https://forum.unity.com/threads/lay...ke-described-in-the-docs.744302/#post-6355392
     
  4. Techmologue

    Techmologue

    Joined:
    Nov 21, 2017
    Posts:
    41
    Thank you, Kurt.

    I have made a quick video about what is happening here:


    my terminology needs some work, along with my English

    Code (CSharp):
    1. laserBeam.SetPosition(0, laserSpawnPoint.transform.position);
    2.             int layerMask2 = 1 << 6; //6=shotLayer
    3.             layerMask2 = ~layerMask2;
    4.             RaycastHit laserHit;
    5.             if (Physics.Raycast(laserSpawnPoint.transform.position, laserSpawnPoint.transform.forward, out laserHit, Mathf.Infinity, layerMask2))
    6.             {
    7.                 if (laserHit.collider)
    8.                 {
    9.                     if ((laserHit.collider.tag=="enemy")
    10.                         && (laserTimer==1f))
    11.                     {
    12.                         if (laserHit.collider.GetComponent<scrAI>().energy>0)
    13.                         {
    14.                             laserHit.collider.GetComponent<scrAI>().energy -= 4;
    15.                         }
    16.                         Debug.Log("blah laser");
    17.                     }
    18.                     laserBeam.SetPosition(1, laserHit.point);
    19.                     laserLightEP.transform.position = laserHit.point;
    20.                 }
    21.                 else
    22.                 {
    23.                     laserBeam.SetPosition(1, laserSpawnPoint.transform.forward * 5000);
    24.                 }
    25.             }
     
    Last edited: Jan 26, 2023
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    This ^ ^ ^ stands out as a potential problem unless you take extraordinary measures to ensure it WILL be 1f.

    Floating (float) point imprecision:

    Never test floating point (float) quantities for equality / inequality. Here's why:

    https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html

    https://forum.unity.com/threads/debug-log-2000-0f-000-1f-is-200.1153397/#post-7399994

    https://forum.unity.com/threads/why-doesnt-this-code-work.1120498/#post-7208431

    "Think of [floating point] as JPEG of numbers." - orionsyndrome on the Unity3D Forums
     
    Techmologue likes this.
  6. Techmologue

    Techmologue

    Joined:
    Nov 21, 2017
    Posts:
    41
    How did I do that?!
    Well, I guess there is two things going on:
    1) I am loading 1f into the laserTimer float variable, every time I hit the fire button. ...intentionally to reset the timer (edit: if the timer ran out, that is)
    2) ...as you probably have guessed it's being manipulated by "Time.deltaTime" after this single frame of computation has ended (hence why I trust that my 1f stays 1f for 1frame). Keeping floats floating together...

    other than that >= would have been my first choice, but I got a bit wild

    lol, love the quote though
     
    Last edited: Jan 26, 2023
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    The next thing that stands out is this ^ ^ ^

    Are you really using layer 6? In my experience the first 8 layers are reserved.

    Screen Shot 2023-01-26 at 6.23.27 AM.png

    You can only start text-defining with Layer 8 and on upwards... now you MIGHT be able to set layer 6 in pure code, but it also might not do what you think. I'd stick with user-definable layers.

    In any case, you're gonna have to debug to find out what's going on, step by step and find what decision is failing your code out.

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
    Techmologue likes this.
  8. Techmologue

    Techmologue

    Joined:
    Nov 21, 2017
    Posts:
    41
    That did it!
    I have "Debug.Log"ed out the tag of another cube directly placed inside the cube I wanted to be ignored (doh!), and the line renderer and code worked fine always... There was just another object in the way which I totally forgot about since it is invisible.

    classic! oops
     
    Kurt-Dekker likes this.