So I have created a flashlight where it can be turned on and off (pressing q) But I want my player to be able to run faster while the flashlight is turned off. any ideas?
if (flashlight.active)
{
maxSpeed = 1;
}
else
{
maxSpeed = 2;
}
I think this should work.
static var maxSpeed = 0;
private var lightOn = false;
Update () {
if (Input.GetKeyDown(KeyCode.Q)){
if(lightOn==false){
lightOn = true;
maxSpeed = 1;
}
else{
lightOff = false
maxSpeed = 2;
}
}
}
Now, use this code for Move.js…
#pragma strict
static var maxSpeed : float;
public var player : CharacterController;
public var mouseSensitivityX : float;
private var maxSpeedString : String;
function Start () { }
function Update () {
maxSpeedString = String.Format ("Max Speed : {0}", Move.maxSpeed.ToString());
transform.Rotate (0, Input.GetAxis ("Mouse X") * mouseSensitivityX * Time.deltaTime, 0);
if (Input.GetKey (KeyCode.W)) {
player.Move (transform.TransformDirection (Vector3.forward) * Move.maxSpeed * Time.deltaTime);
}
}
function OnGUI () {
(GUI.Label (Rect (25, 25, 100, 75), maxSpeedString));
}
…and for Flashlight.js
#pragma strict
public var flashlight : Light;
public var isLightOn : boolean;
function Start () { }
function Update () {
if (Input.GetKeyDown (KeyCode.Q))
isLightOn = !isLightOn;
if (isLightOn) {
flashlight.enabled = true;
Move.maxSpeed = 5;
} else {
flashlight.enabled = false;
Move.maxSpeed = 8;
}
}
I’ve added another script too…
#pragma strict
public var mouseSensitivityY : float;
function Start () { }
function Update () {
transform.Rotate (Input.GetAxis ("Mouse Y") * mouseSensitivityY * Time.deltaTime, 0, 0);
}
I converted maxSpeed into a string so you can see how fast you are going.
This is the link to the updated project…