Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

FlashLight

Discussion in 'Scripting' started by CP_luca, Jul 28, 2022.

  1. CP_luca

    CP_luca

    Joined:
    Mar 17, 2022
    Posts:
    1
    Vorrei creare una torcia con una luce fissa, ma con l'avvicinarsi del nemico la torcia deve iniziare a spegnersi e accendersi a intermittenza, qualcuno può aiutarmi? Non riesco a creare lo script
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    First and foremost: if you want someone to answer your question on a forum, you need to make it easy for that person to answer. This means describing your problem in as much detail as possible, but also, writing it in a language that will make it easy for that person to understand.

    Most people on this forum speak english, so anyone willing to answer your question will need to copy your text and go to google translator, italian->english. The time it takes for you to do the same in the opposite direction is minimal, you save time to a lot of people, and greatly increases the chance someone will actually answer. Whats not to like? :)
    (I'm spanish btw, so I can understand your question reasonably well without the need to translate it).

    Regarding your question: you will need to calculate the distance between your light and the enemy (Vector.Distance(), passing the position of the enemy and the position of the light). Then when the distance becomes small enough, use some Random value to toggle the light on/off and make it flicker: you call Random.value, and if the value is smaller than the probability of the light to go off, you turn it off. Otherwise, you turn it on.

    That's the gist of it, assuming you know some basic C#. If you have trouble with the basic syntax of C#, I'd recommend looking for C# tutorials since that will allow you to progress faster in your game.
     
    Last edited: Jul 29, 2022