Distance sensor

How can I make a distance sensor to change the light intensity?

This is a pretty vague question. I’m going to assume you have an actor with a light component and you want the light intensity to get brighter as the (single) player approaches it.

First, you need to get the distance the player is away from the light. To do this, you can use a “Get player controller” node (with a 0) and call “Get controlled pawn” from there. Then, from the pawn, use a “Get actor location”. Drag the light component into your graph and use a “Get actor location” on that as well. From the two vectors, use a subtraction node to subtract them. From the resulting difference, use a “Get vector length”. This will be the number of units (cm) away the player is from the light.

In order to convert this distance into a resulting light intensity, I would probably use a LERP node (float) with the minimum being 0 and the maximum being the intensity you wish to use when the player is practically on top of the light (the brightest intensity you want to use). You can feed the result of this into a “Set Intensity” node from the light component.

The last piece is using the distance the player is away from the light and turning it into an alpha for the LERP node. For this, you should determine the distance the player should be from the light when it starts getting brighter. I’m going to use 1500 units as an example and call it MaxDistance. So if the player is > 1500 units away, the light will be 0 intensity and will get brighter the closer the player is while under 1500 units. So if you take this MaxDistance value and subtract the vector length result from earlier then take that value and divide by the MaxDistance. This will give you a value between 0 and 1 if the player is within 1500 units from the light and it will be higher the further away the player is. It will give you a negative value if the player is > 1500 units away which is fine - the LERP will effectively ignore this, I believe. So take this result and pass it into the alpha for the LERP and … I think that’s it!

So, just to give an example since I’m not always the best at explaining things:

  1. We get the two vectors for the player location and the actor location. Subtract them and use Get Vector Length to get the distance. Let’s say there is 300 units between the player and the actor.
  2. Create a LERP node using 0 as the min and 1000 as the max.
  3. We use 1500 units as the MaxDistance (probably set this up in a variable for the actor so it can easily be adjusted).
  4. 1500 - 300 = 1200. 1200 / 1500 = .8
  5. Using .8 as the alpha for the LERP, it will result in 800 (80% of the max intensity of 1000) for the intensity.

If the player was 10 units away, 1500 - 10 = 1490 / 1500 = .9933333 which results in 993.3333 (93.33% of the max intensity of 1000)
If the player was 2000 units away, 1500 - 2000 = -500 / 1500 = -.333 which is less than 0 and will result in the LERP returning the minimum value, I believe.

Hope this helps!