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?
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 Physics.Raycast() because it contains many poorly-designed overloads:
How did I do that?!
Well, I guess there is two things going on:
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)
…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
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 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:
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.