Lights out - help needed

I thought I’d just put this out there in case somebody has a quick answer (i.e a line or two of script) to my scenario.

I have a laser beam which has a point light attached to the end that hits things (hit.point)

This lights up the scenery around where the laser hits.

Problem:

The actual object that the laser hits (the ground, or a wall) doesn’t light up - because the light at the hit.point is exactly touching the object when it actually needs to stop a little short of the surface.

I figure I need to take the distance between the laser hit.point and the source of the laser and then move the light back towards the source a little, in order for the hit faces to be lit by the light.

Anyone with a snappy suggestion (even pointing me to the right page in the Unity manual) will save my brain some effort tonight when I eventually get the kids to bed and start up my Macbook after a long day in my boring day job.

Thanks.

Can’t you make the collider a little bigger?

Hmmmm… that might work but it would affect all collision detection in the level. Not sure I want my level scenery to have a collider that doesn’t quite fit. Still, I’m going to try that as it will only take a second :wink:

Looking into using Vector3.Distance right now.

Scaling the collider doesn’t work, because it’s complex geometry with normals pointing in all directions (some inwards, some out), and also several different objects.

So I think I need to get the distance the laser beam extends and then subtract a little and move the light at the end of each update…

You could also do a little vector math:

LaserStartPosition - HitPointPosition gives a Vector pointing from hitpoint to start. Normalize that vector to bring it to a length of 1 unit and add it to HitPointPosition. Set that as position of your light and it sits on the laser one unit before the hitpoint.

You can move the light back along the collision normal.

light.transform.position = collision.contacts[0].point + (collision.contacts[0].normal * movementFactor);

Where light is your light, collision is the Collision object you get from OnCollision(Enter/Exit/Stay), and movementFactor is some float that will determine how far back the light will move. If you’re raycasting replace “collision.contacts[0]” with your RaycastHit object.

Thanks for both those suggestions!

@B-joe, it works :slight_smile:

I meant making the laser’s collider bigger, but yeah, moving it back might be a better solution.

Glad I could help, but I think you should give the solution burnumd posted a try also. It takes fewer code and depending on the shape of your target and from where it’s hit it could also look more realistic.

thanks everyone,

I will try out everything to see what works best, time permitting :slight_smile:

The free version of my app was accepted for release by Apple yesterday, so I am full out on putting in the extra levels now for the full version!

Thanks Burnumd,

in the end I opted for your solution, modified for raycasting…

LaserLight.transform.position = Hit.point + Hit.normal*0.2;