- Home /
help with Flashlight Pickup
First and formost- I need the coding to be in JAVASCRIPT not C SHARP
Hey Guys, I've read a couple other questions related to this, but they both involved batteries. I don't want batteries for the flashlight in my game. I just want for the player to be able to walk over to a table, and click E to pickup the flashlight. BUT I do want them to be able to turn it on and of with the F key. I already have the script for the flashlight turning on and off writen, I just dont now how to incorporate the whole PICKUP part off it where they wont me able to turn it on until they pick it up. This is what I have so far.
private var lightSource : Light; var soundTurnOn : AudioClip; var soundTurnOff : AudioClip;
function Start () { lightSource = GetComponent(Light);
}
function Update () {
if (Input.GetKeyDown(KeyCode.F)) ToggleFlashLight();
}
function ToggleFlashLight () {
lightSource.enabled=!lightSource.enabled;
//Audio if (lightSource.enabled) {
audio.clip = soundTurnOn;
} else {
audio.clip = soundTurnOff;
}
audio.Play();
}
@script RequireComponent(AudioSource) @script RequireComponent(Light)
Answer by Jeejo · Jun 18, 2013 at 09:35 PM
For picking up the flashlight, I would do something like this:
 var worldModel : GameObject;
 var viewModel : GameObject;
 var actualLight : Light;
 var hasLight : boolean = false;
 
 function OnTriggerEnter (other : Collider) {
     if(other.name == "player name here") {
         hasLight = true;
         Destroy(worldModel);
         Destroy(this);
     }
 }
 
 function Update () {
     if(hasLight == true) {
         viewModel.active = true;
         actualLight.enabled = true;
     }
     else {
         viewModel.active = false;
         actualLight.enabled = false;
     }
 }
As for turning it on and off, I would make another script.
 var actualLight : Light;
 var on : boolean = true;
 var soundTurnOn : AudioClip;
 var soundTurnOff : AudioClip;
 
 function Update () {
     if(Input.GetKeyDown("f")) {
         on = !on;
         if(on == true) {
             audio.PlayOneShot(soundTurnOff);
         }
         else {
             audio.PlayOneShot(soundTurnOn);
         }
         
     }
     if(on == true) {
         actualLight.enabled = true;
     }
     else if(on == false) {
         actualLight.enabled = false;
     }
 }
what do I assign each of those scripts to? The player? The Spotlight?
Assign the first script to the trigger, and assign the second script to the player.
Ok, well I got it to where the player walks into the trigger and the flashlight vanishes and the spotlight turns on, but there are a few changes I would like to make. The "toggle flashlight" scipt isnt turning it on and off for some reason, and I want to make it to where the player presses a button to pickup the flashlight, so its not done automatically
Ok Ok, Now I have it to where the toggle works, but you can make the on and off sounds before you pick up the flashlight, how to do i change this?
Sorry about late response, but you can access the different scripts by doing something like this:
 var player = GameObject.Find("player name here");
 var onOff = player.GetComponent(2d script name here);
 
 if(hasLight == true) {
     onOff.enabled = true;
 }
 else {
     onOff.enabled = false;
 }
Your answer
 
 
             Follow this Question
Related Questions
Could anyone help me with .js code to be able to pickup a flashlight and turn it on and off -1 Answers
Toggle button help 1 Answer
Flash light toggle problem 1 Answer
FlashLight Fixes Please?? 1 Answer
How to pickup and equip an object? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                