Tuesday, 12 November 2013

Player Light Script

using UnityEngine;
using System.Collections;

public class PlayerLightLerp : MonoBehaviour
{
    public float smooth;

public bool lightOn = false;

public int particleTime;

    public bool timerExpired = false;
 
private float newIntensity;

public bool timerStarted = false;

public int lightTimer = 0;

public float cooldownPeriod = 0f;

public CharacterParticle scriptForParticles;

public CharacterParticle scriptForParticles2;


    void OnGUI() {
        GUI.HorizontalSlider(new Rect(25, 25, 100, 30), lightTimer, 0.0F, 10.0F);

    }


// same as if( timerStarted == false)

IEnumerator PollTimer()
{
if(!timerStarted)
{
StopCoroutine("CountUp");
timerStarted = true;

timerExpired = false;

bool stopTimer = false;

while (!stopTimer)
{

if(lightOn)
{
lightTimer--;

if(lightTimer <= 0)
{
stopTimer = true;
}

}
else
{
lightTimer++;

if(lightTimer >= 10)
{
stopTimer = true;
}
}



yield return new WaitForSeconds (1);
}

//yield return new WaitForSeconds(lightTimer);


timerExpired = true;

timerStarted = false;
}

}

IEnumerator CountUp()
{
while( lightTimer < 10 )
{
lightTimer++;
yield return new WaitForSeconds (1);
}
}




void Awake ()
    {
particleTime = 10;
       
        newIntensity = light.intensity;
       
    }
   
   
    void Update ()
    {
       

IntensityChanging();



        if(Input.GetKeyDown(KeyCode.F))
{
lightOn = !lightOn;

}

       
    }
   
   
   
    void IntensityChanging ()
    {
        float intensityA = 0.0f;
        float intensityB = 1.0f;
       
       


if ((lightOn == true) && (cooldownPeriod < Time.time ))
{
newIntensity = intensityB;

cooldownPeriod = Time.time+ 10;

scriptForParticles.EnableParticles (true);
scriptForParticles2.EnableParticles (true);

if(!timerStarted)
{
lightTimer = 10;
StartCoroutine (PollTimer());

}

}

if(timerExpired)
{
timerExpired = false;
lightOn = false;
scriptForParticles.EnableParticles (false);
scriptForParticles2.EnableParticles (false);

StartCoroutine( "CountUp" );
}

if (lightOn == false)
{
newIntensity = intensityA;
scriptForParticles.EnableParticles (false);
scriptForParticles2.EnableParticles (false);
}


       light.intensity = Mathf.Lerp(light.intensity, newIntensity, smooth * Time.deltaTime);
    }
   
   

}

This is the C# script that changes the intensity of the players light with a toggle button. A cool down has also been built into the script stopping the player using it forever and without waiting for a few seconds.


As you can see the playerlight script has been assigned to the player light in the graphic of the first person controller

No comments:

Post a Comment