Spotlight Cone Angle

Hello,

I have a spotlight shining down on top of my First Person Controller and I want to make the cone angle of the spotlight increase as the player moves forward using the “up” arrow key.

Does anyone have any suggestions on how I can achieve this? I am still new to scripting, since my focus is animation in Maya. Any information is greatly appreciated! Thanks!

–Velk

You could attach code like the following to the spotlight object:

function Update () {
    // are we moving forward?
    if (Input.GetAxis("Vertical") > 0) {
        // if so, increment the spot angle by 1 degree per second
        light.spotAngle += 1 * Time.deltaTime;
    }
}

You may want to play around with the Input Manager if the up arrow is the only key you want to check for, but generally this should do what you’re after.

Wow! Thank you so much! That works really well =)

Is it also possible to have the spot angle decrease if the player slows down or stops moving altogether?

Basically, I am trying to create a spotlight that increases as the player continues to run, but also DECREASES when the player slows down or comes to a stop.

Any information is greatly appreciated! Thank you!

–Velk

Working from the code I posted earlier, we can add that functionality:

var deltaAngle : float = 1.0 // change in spot angle measured in degrees per second

function Update () {
    // is the player trying to move forward?
    if (Input.GetAxis("Vertical") > 0) {
        // if so, increment the spot angle by deltaAngle degrees per second
        light.spotAngle += deltaAngle * Time.deltaTime;
    // is the player trying to move backwards?
    } else if (Input.GetAxis("Vertical") < 0) {
        // if so, decrement the spot angle by deltaAngle degrees per second
        light.spotAngle -= deltaAngle * Time.deltaTime;
    }
    // do nothing if the player is neither accelerating nor decelerating
}

If you want to do other fancy things, like change the spot angle based on the player’s velocity, you’d need access to that rigidbody. But this is just a starting point.

Thank you so much! I really appreciate it! =)