- Home /
Running faster while flashlight is off
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?
Answer by Kiloblargh · Nov 03, 2012 at 04:50 PM
if (flashlight.active)
{
maxSpeed = 1;
}
else
{
maxSpeed = 2;
}
Hmm, how do i use it? heres my script for a flashlight, but i don't know how to add yours to make my player run faster.
function Update () {
if (Input.Get$$anonymous$$eyDown("q")){
if (light.enabled == true) light.enabled = false; else
light.enabled = true;
}
}
You need a script for player movement. Create a static variable of maxSpeed within it.
Rather than using 'if (light.active == true)', use 'if (light.active)'. They mean the same thing.
like this?
static var maxspeed;
function Update () {
if (Input.Get$$anonymous$$eyDown("q")){
if (light.enabled == true) light.enabled = false; else
light.enabled = true;
if (light.active)
{
maxSpeed = 1; } else { maxSpeed = 2; }
}
}
You have to set that boolean as a variable outside of Update. Always set it false.
var lightOn = false;
Answer by Althaen · Nov 04, 2012 at 04:12 PM
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;
}
}
}
It's a good idea to define your variable type. Also, booleans default to false.
Answer by Coreyf716 · Nov 04, 2012 at 02:50 PM
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...
The link is to the project. You can export the scripts into your project. Let me know if you have any questions. Good luck! :)
I added line of code that allows your player to look around. The code has been tested. It all works now.
Ill try it in a bit, im working on another thing for it right now
Ok. Tell me what you think of the project. It will explain everything.
Flashlight: Assets/Flashlight.js(15,8): BCE0005: $$anonymous$$ identifier: '$$anonymous$$ove'.
You must name the movement script "$$anonymous$$ove". Also, make sure that maxSpeed is a static variable.