- Home /
Pointlight changing colour with distance
Okay I'm a total noob so if you decide to help me just treat me as a very simple child. I've completed a Pong game as part of a tutorial and I'm interested in playing around with it, I've added a point light to the ball so it has a basic glow but I was wondering how to go about making it fade to a different colour in proximity to the player and enemy paddles and then fade back when the ball bounces away.
Giving me the script won't teach me anything so I was hoping for some sort of pseudo-script or something to point me in the right direction. So far I've played with using Mathf.PingPong from the Unity manual to fade the colour, I just need it to be distance based. Things like raycasting (if that's applicable here) are waaaay out of my league for the moment!
Answer by robertbu · Mar 11, 2013 at 03:54 PM
Someone who doesn't want a script. How refreshing.
Get access to the paddles inside the 'ball' script. You can add a public game object or transform at the top of the 'ball' script and drag and drop the paddles onto the variables. Or you can use private variables and use GameObject.Find() to get access. See Accessing Other Game Objects. for more info.
Calculate the distance to each paddle from the ball. One way is to use Vector3.Distance().
Calculate a fraction between the paddles. The calc will be something like "distance1 / (distance1 + distance2).
Create and set two color variables, one for each paddle. If you make them public (default in Javascript), you can set the color in the inspector, or you can directly assign the color in script.
Use Color.Lerp() to generate the color.
Assign the color to the light (Light.color).
Answer by wgibbsw · Mar 11, 2013 at 04:13 PM
Fantastic! Thanks for your reply I'll look into these and see where it leads me. There's one thing I don't get at the moment though, what's the fraction between paddles calculation for?
You feed that as the fraction to the Color.Lerp(). Color.Lerp() will linearly interpolate between the two colors. So if the color assigned to one paddle was red, and the color assigned to the other was blue, when the distance between the paddles is even, you will have a purple light.
As I reread your question, you may only want it to light when it is near one of he paddles. The algorithm is almost the same.
In additions to the two colors above, define a third neutral color. Test the distance to each paddle each frame. If the distance falls below some predefined limit, then you Color.Lerp() between the neutral color and the paddle color. Pseudo code:
if (distance < limit)
light.color = Color.Lerp(paddleColor, neutralColor, distance/limit);
else
light.color = neutralColor;
Spot on thanks very much this'll keep me out of trouble for a little while.
Your answer
Follow this Question
Related Questions
Color lerp once? 2 Answers
Duplicated objects, different colors 1 Answer
Is changing the color of GUIText unavailable in the Free Edition? 1 Answer
On Touch change color 2 Answers