• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by wgibbsw · Mar 11, 2013 at 03:41 PM · colorbeginnerchangefadecolour

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Mar 11, 2013 at 04:34 PM 0
Share

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;
avatar image wgibbsw · Mar 11, 2013 at 04:39 PM 0
Share

Spot on thanks very much this'll keep me out of trouble for a little while.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Duplicated objects, different colors 1 Answer

Color lerp once? 2 Answers

3D Text - How to change colour - Doesn't seem to work 2 Answers

How to change color as flashing or blink? 0 Answers

Horizontal Slider Normal-Background Change Color Independently 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges