- Home /
How to make a flashlight which is ON after collect?
Hello! Im making horror game. I want to make a flashlight which will be ON (till the end of the game) after collecting it from shelf. I know how to make a normal flashlight (attach spotlight to player or something else) but i dont have any script for it etc etc. If u want u can link here free flashlight model too :P Thanks in advance :)
Answer by kog513 · Jul 17, 2018 at 03:58 PM
I'm new here but I got this simple idea , u creat two flashlights , one set it inactive at the start of the game and make it already attached to the player and the other one in the shelf or whatever u wanna put it , anyway add a bool variable to your player and add a methode which only functions if the bool is ture that sets the first flashlight to active .. one ore thing u can destroy the one you collected attach this script to your Character .
public class FlashLight: MonoBehaviour {
public GameObject FlashLight // the one attached to the player
private bool FlashLight_collected ;
void Start() {
FlashLight.SetActive(true) ;
}
void Update(){
/* The following code means ones u collected the FlashLight the one attached to the player will be
set to active */
if (FlashLight_collected && !FlashLight.active){
FlashLight.SetActive(true) ;
}
}
void OnTriggerEnter(Collider collider){
if (collider.CompareTag("FlashLight")) {
DestroyObject(collider.gameobject) ;
FlashLight_collected = true ;
// you can improve it by setting a bool to true and once u r out of the colliders it set it to false and
add another methode which allows u to click on a specific key to collect that Flashlight
}
}
} I'm new as well and I'm not used to write stuff in the forum so I'm sorry if I wasnt clear enough or if I had some mistakes in the code . Try it ur self just copy the idea and make ur own code doesnt matter if it's unoptimized just try to understand and learn how to code . I'm like 18 days in unity and I never coded before , Only some python in the uni but I can apply most of my ideas now
Yea man but im that type of newbie can u explain it more? xD
Okay I'll do my best , think about it this way : U already have the flash light but u wont be able to use it until u collect another one , So the Idea is to set the one attached to u as inactive(like invisble and wont function) at the beginning of the game , and then when u collect one in game , u set it to active(visible and functions) .
-First creat a flashlight and make it a child to ur player , and set to inactive ( in the inspecter ) - creat another flashlight and position it wherever u want -now make a sphere collider and set it to trigger to the flashlight u will be collecting ,and give it a tag like "FlashLight" ( give it any tag u want ) -now creat a script and attach it to ur player So u need 2 variables : - GameObject FlashLight // the one attached to u since the game started so make it a public one and drag and drop ur flash light in it . - Bool CollectedFlashLight // which will decide if the FlashLight attached to u should be active or not now in the script creat a methode like this one :
void FoundFlashLight(){
if (collectedFlashLight && !FlashLight.active) {
/* Just saying , if u dont know this : if u apply "!" to a bool variable that means setting it to the opposit value in this case u r checking if it's false (which means our flashlight , the one attached to ur player is not active)*/
FlashLight.SetActive(true) ;
}
}
now we need to check if we collected the FlashLight or not by making OnTriggerEnter(Collider collider) methode this methode takes a Collider Variable as a parametre , the collider variable is whatever u collide with but if this collider is set to trigger this means u can enter in the collision range , sry I'm not a good explainer ..
so we need to add this code :
void OnTriggerEnter(Collider collider){
if (collider.CompareTag("FlashLight")) {
// write the tag u attached to the flashlight u will be collecting
destroyObject(gameobject) ; // Since we no longer need it
collectedFlashLight = true ;
}
}
now the final step call the FoundFlashLight() methode in ur update finction ! and voila done I'm sry mate I wish I could contact u in a better way to explain the whole thing perfectly . but u need to check for OnCollisionEnter() , OnTriggerEnter , If , while , RayCast and the common variables like Collider , Collision , int , float , string , RayCastHit , Some Input methodes ect .. start slowly mate just search and write stuff in a paper try to Creat something through what u know , and try each time to invent new ideas from the same way of Coding , I'm Creating a Game and it's pretty awsome so far , even tho I'm not that good with coding and stuff , but I never copy others scripts .. if u want to contact me I may can help u learn how to code , but u need to be really patient and spend some time trying
Answer by singingstranger · Jul 17, 2018 at 03:57 PM
I have never made this myself but perhaps if you made the condition of it being on or off attached to a bool? So basically:
void Start(){
lampon=false;
}
void Update(){ if (pickuplamp=true){ lampon=true; } } ?
Yea man but im that type of newbie can u explain it more? xD
Your answer
Follow this Question
Related Questions
Flashlight 1 Answer
Pressing "Fire2" Spawns and removes object on click 0 Answers
Developping for Android [Tutorials] 0 Answers
How to get started with a horror command prompt / terminal game? 1 Answer
Could anyone help me with .js code to be able to pickup a flashlight and turn it on and off -1 Answers