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

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

Can the LineRenderer even be configured to ignore objects that have a certain layer mask assigned?

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://discussions.unity.com/t/828929/4

https://discussions.unity.com/t/801152/2

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

Raycasting in 2D:

https://discussions.unity.com/t/829336/4

The difference between Layers vs LayerMasks:

https://discussions.unity.com/t/802897/2

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

https://discussions.unity.com/t/758115/8

[quote=“Kurt-Dekker”, post:3, topic: 906976]
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://discussions.unity.com/t/828929/4

https://discussions.unity.com/t/801152/2

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

Raycasting in 2D:

https://discussions.unity.com/t/829336/4

The difference between Layers vs LayerMasks:

https://discussions.unity.com/t/802897/2

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

https://discussions.unity.com/t/758115/8
[/quote]
Thank you, Kurt.

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

https://www.youtube.com/watch?v=V-Q_Kkfi_fU

my terminology needs some work, along with my English

laserBeam.SetPosition(0, laserSpawnPoint.transform.position);
            int layerMask2 = 1 << 6; //6=shotLayer
            layerMask2 = ~layerMask2;
            RaycastHit laserHit;
            if (Physics.Raycast(laserSpawnPoint.transform.position, laserSpawnPoint.transform.forward, out laserHit, Mathf.Infinity, layerMask2))
            {
                if (laserHit.collider)
                {
                    if ((laserHit.collider.tag=="enemy")
                        && (laserTimer==1f))
                    {
                        if (laserHit.collider.GetComponent<scrAI>().energy>0)
                        {
                            laserHit.collider.GetComponent<scrAI>().energy -= 4;
                        }
                        Debug.Log("blah laser");
                    }
                    laserBeam.SetPosition(1, laserHit.point);
                    laserLightEP.transform.position = laserHit.point;
                }
                else
                {
                    laserBeam.SetPosition(1, laserSpawnPoint.transform.forward * 5000);
                }
            }

[quote=“Techmologue”, post:4, topic: 906976]
(laserTimer==1f)
[/quote]

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://discussions.unity.com/t/851400/4

https://discussions.unity.com/t/843503/4

“Think of [floating point] as JPEG of numbers.” - orionsyndrome on the Unity3D Forums

1 Like

[quote=“Kurt-Dekker”, post:5, topic: 906976]
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://discussions.unity.com/t/851400/4

https://discussions.unity.com/t/843503/4

“Think of [floating point] as JPEG of numbers.” - orionsyndrome on the Unity3D Forums
[/quote]
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

[quote=“Techmologue”, post:4, topic: 906976]
1 << 6;
[/quote]

The next thing that stands out is this ^ ^ ^

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

8758543--1187155--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://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

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://discussions.unity.com/t/839300/3

When in doubt, print it out!™

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

1 Like

[quote=“Kurt-Dekker”, post:7, topic: 906976]
The next thing that stands out is this ^ ^ ^

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

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://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

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://discussions.unity.com/t/839300/3

When in doubt, print it out!™

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.
[/quote]
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

1 Like