Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    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


    Nov 21, 2017
    Solved! I have made a classic mistake


    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

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


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


    Mar 16, 2013
    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:

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

    Raycasting in 2D:

    The difference between Layers vs LayerMasks:

    Always use named arguments with
    because it contains many poorly-designed overloads:
  4. Techmologue


    Nov 21, 2017
    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


    Mar 16, 2013
    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:

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


    Nov 21, 2017
    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) 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


    Mar 16, 2013
    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
    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

    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: or this answer for Android:

    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:

    When in doubt, print it out!(tm)

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


    Nov 21, 2017
    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.