Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by SuperSparkplug · Apr 02, 2013 at 08:27 AM · lightflashpulse

How do I animate a pulsing light?

Hi, so I haven't really worked with animating lights before, and am trying now with Unity. I have my entire program working, but as an added effect, I want to make it so that, on button press, a point light variable attached to my Particle System will have its range quickly animate from 0 to a chosen number (Let's just say 5) and go back to 0, creating a sort of pulsing or flashing light effect. However, I want it to animate it so that there's a smooth transition between the start and end points of the animation within a short span of time. While I know how to access the range functions, I'm not exactly sure how to code this.

Can anyone help me with this? (Preferably in C#)

alt text

  • want the light in that centre to animate out and in on button press*

screen shot 2013-04-02 at 2.09.14 am.png (35.5 kB)
Comment
Add comment · Show 2
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 · Apr 02, 2013 at 04:38 PM 0
Share

If you a linear transition is okay, you can use $$anonymous$$athf.PingPoint() to create a flashing effect. If you need a more eased transition, you can use $$anonymous$$athf.Sin().

avatar image SuperSparkplug · Apr 03, 2013 at 12:05 AM 0
Share

I don't know how to code that though. I'm not a good programmer. What should I do here?

 using UnityEngine;
 using System.Collections;
 
 public class LightFlashC : $$anonymous$$onoBehaviour {
 
     public float lerpTime = 0.5f;
     private float i = 0;
      
     
     // Use this for initialization
     void Start () {
         light.range = 0;
     }
     
     // Update is called once per frame
     void Update () {
         
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))    {
     light.range = $$anonymous$$athf.Sin(5,10);
      
     i+= Time.deltaTime;
         }
      
     }
     
     }

1 Reply

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

Answer by robertbu · Apr 03, 2013 at 12:21 AM

Based on your use of light.range above, here is a script that when attached to a point light scales the range:

 #pragma strict
 
 var maxDist = 30;
 var speed = 40.0;
 
 function Update () {
     light.range = Mathf.PingPong(Time.time * speed, maxDist);
 
 }
Comment
Add comment · Show 14 · 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 SuperSparkplug · Apr 03, 2013 at 02:48 AM 0
Share

That doesn't exactly work the way I want it to. It just suddenly becomes a random range under 30 and it doesn't animate itself back to 0.

Also, I don't understand the meaning behind time.time * speed.

avatar image robertbu · Apr 03, 2013 at 03:06 AM 0
Share

Do this setup:

  • Start with an empty scene.

  • Put a cube at (0,0,0) (with the default material).

  • Create a point light at and place it at (0,5,0)

  • Attach this script above to the light

  • Hit play

What it does is animate the range of the light. It cycles from 0 up to maxDist and then back down to 0 repeatedly. It is easy to see with this setup. You may want to animate the intensity ins$$anonymous$$d. If so, I'd start with a maxDist setting of 8 and a speed setting of 10.

Time.time drives the PingPong. 'speed' deter$$anonymous$$es how fast. 'speed' is not an absolute value, but ins$$anonymous$$d is relative to maxDist. For any given value of maxDist, a higher value of 'speed' will make the light cycle faster.

avatar image SuperSparkplug · Apr 03, 2013 at 03:20 AM 0
Share

Ah okay. I tried it in an empty scene. THAT'S essentially how I want it to work... Still, it doesn't work within the confines of my script. WITHOUT an if statement for the button press, it works fine pulsing automatically. Whenever I use an if statement to wait for a button, however, the range is random between the range and it stays constantly in that range unless I press my button again. Why is that?

Here's my code as it is.

 using UnityEngine;
 using System.Collections;
 
 public class LightFlashC : $$anonymous$$onoBehaviour {
 
     public float lerpTime = 0.5f;
     private float i = 0;
     
     public int maxDist = 5;
     public float speed = 40.0f;
      
     
     // Use this for initialization
     void Start () {
         light.range = 0;
     }
     
     // Update is called once per frame
     void Update () {
 
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))    {
     light.range = $$anonymous$$athf.PingPong(Time.time * speed, maxDist);
      
     i+= Time.deltaTime;
         }
     }
      
     }
avatar image robertbu · Apr 03, 2013 at 04:06 AM 0
Share

I'm not sure how you want this to work. If you only want it to flash when the button is down, you need to use Input.Get$$anonymous$$ey() rather than Input.Get$$anonymous$$eyDown(). Input.Get$$anonymous$$eyDown() only returns true for the single frame it the key is down. In addition, you probably want to use your own timer. The issue with using Time.time and start and stopping is that Time.time continues to march one so when you restart your flasher, you don't know where it is in the cycle. You may want something like:

 using UnityEngine;
 using System.Collections;
 
 public class FlashLightC : $$anonymous$$onoBehaviour {
     public  float maxDist = 5.0f;
     public  float speed   = 40.0f;
     private float timer   = 0.0f;
     
     void Start () {
        light.range = 0;
     }
     
     void Update () {
         if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))    {
             light.range = $$anonymous$$athf.PingPong(timer * speed, maxDist);
             timer += Time.deltaTime;
        }
     }
 }
avatar image SuperSparkplug · Apr 03, 2013 at 04:12 AM 0
Share

Hmm... After all these changes, it doesn't seem to fix things. On button press, the light is still static and unchanging until I press the button again. It doesn't animate itself back to 0 like it would without the if statement... =/

Show more comments

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.

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

(Resolved) Help on creating a Camera Flash (script provided) 1 Answer

Animating the Render Component Halo 0 Answers

Why isn't my flashing script working? 1 Answer

Cannot Get Light To Flash - Help 1 Answer

Does anyone know of some good scripting tutorials? 2 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