Flashlight?

Hi, I need a script to apply to a spotlight so that It will turn on when the ‘f’ key is pressed, and off again, to represent a spotlight.
Anyone?

Make an empty gameobject and apply a script that checks for when the user presses the ‘f’ key. Then when the key is pressed, deactivate the children to that empty gameobject. Make the spotlight a child of the empty gameobject.

Lol, I literally just tried to make and completed a flashlight script before you posted 8).

Do what the guy above me said, and if you don’t like that method of the flashlight script try making the intensity set to whatever. So when you press f, the intensity either increases or decreases depending on what it did last time.

Hope I helped.
:smile:

instead of an empty gameObject, why not the flashlight(cylinder) itself?

The above example was referring to if you did not want to use the cylinder, but both will work.

If you have a script that is attached to the flashlight that deactivates itself when you press the ‘f’ key, then when you press the ‘f’ key again, it won’t go back on. Because the script is deactivated with the flashlight and will no longer check for user input.

ok, heres what I did. I got rid of the game object, it interfered with the script. I set the light to the camara in the right position, then, made it rotate around the y axis 180 degrees, so its not visable to the camara:

var rotate = Vector3(0,180,0);

function Update()
{
	if(Input.GetButtonDown("Flashlight")) {
	transform.eulerAngles += rotate;
	}
}

This works fine, Im only good at writing transforms. :smile:

This won’t work for a multiplayer game though. Why not just set the intensity with the code?

Because I dont know the code to use. It works for a FPS single player game, but not for a third person or multiplayer game. what code do I use to adjust intensity?

Ah, ok. Let me show you :smile: .

var flashlightOn : boolean = false;

function Update () {
	//Checks if the boolean is true or false.
	if(flashlightOn == true){
		light.intensity = 1;//If the boolean is true, then it sets the intensity to what ever you want.
	} else {
		if(flashlightOn == false){
			light.intensity = 0;//If the boolean is false, then it sets the intensity to zero.
		}
	}
	
	//Checks if the F key is down and whether the boolean is on or off.
	if(Input.GetKeyDown(KeyCode.F)  flashlightOn == false){
		flashlightOn = true; //If the f key is down and the boolean is false, it sets the boolean to true.
	} else {
		if(Input.GetKeyDown(KeyCode.F)  flashlightOn == true) {
		flashlightOn = false;//If the f key is down and the boolean is true, it sets the boolean to false.
		}
	}
	
}
}

Sorry for the comments being carried to the next line, they are long. :smile:

What exactly does a boolean do?

never mind, I know now that a boolean is true or false… but sometimes the syntax confuses me, like , ==, and all the {}{}{} its hard to understand what gos where and why.:stuck_out_tongue:

A boolean is a statement of true or false. That is all. Basically, what this script does is check if the boolean is true or false, and does the best action for the value of the boolean.

It seems you are not just a noob to scripting but to programming in general, am I correct?

If so, you may want to learn the very basics of a programming language before continuing. It would only take a couple of hours!

Yea, super noob. I understand basic transforms and JavaScript syntax, but still have a hell lot to learn! What do you think the most complex script in a FPS is?

Lol, well from what I have seen of the mouselook script of C# it might just be that :lol:.

But other than that considering that you don’t have to script that, it completely depends on what you are doing in the game. What type of weapons and is there going to be AI? I would guess AI, but if you are trying to make an FPS you might want to check the tutorial on it! However, only use this as an example because I have heard it is done pretty badly.

Yes, weapons and AI. The AI in the tutorial isent bad, its just simple. As far as Ive seen, a lot of Unity-based Fist Person Shooters arent very good, I guess no one wants to spend too much time on them. However for me, the hardest part would be the GUI and LoadLevel scripts, so that everything the player changed in the previous level, can still be accessed(eg, dead enemys, blood splat, broken crates, general position of objects, ect…) As for AI, there will be a lot of different ones, for each creature, but I want to make a zombie one. Plus, the character and gun animations themselves, what really makes a game realistic, is lacking in other Unity FPS’s, and would probebly be difficult. Wow, I have high ambitions, and It will be quite a while until I can acually get that far. It just makes me feel stupid now… lol :wink:

@ edplane. here is my light switch script i made for my game. its a very simple script.you can turn your light on and off with it.

function Update() {
if (Input.GetKeyDown("f")) {
	if (light.enabled == true)
		light.enabled = false;
		else
		light.enabled = true;
	}
}

OK, It works, but this one enables and disables the light function. I dident even know there was a code to do that! thx!

Why did I not look further down the page of the scripting reference for enabled? Oh well, thanks that helps me too.

Whats with your signature? I would rather learn everything in one second than 4 years! XD well, I guess things just work like that! Any way, whats the point of == instead of just =?