Flashlight battery
How can I do this? I am a complete noob at C#, so all I really know how to do is work UI buttons, do a basic cutscene, and all of the basic stuff. I get how it's done (When this is equal to this time, then flashlight won't work), and I understand partly what a replacement battery does (When this collides with the battery, then flashlight power is full), but I don't know how to code it. Again, complete noob at C#.
I'm only really doing this to add to the horror atmosphere, and not just because every other horror game has this mechanic.
Answer by PunchGoat · Mar 23, 2016 at 08:50 PM
Attach a spotlight to your main camera as a child object, and name it light. Then you're going to want to write a script to toggle that spotlight on and off. Something like this:
using UnityEngine;
using System.Collections;
public class Flashlight : MonoBehaviour
{
public float battery = 20;
void Update()
{
battery = battery - Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Mouse0))
{
if (GetComponent<Light>().enabled == false)
{
if (battery > 0)
{
GetComponent<Light>().enabled = true;
}
}
else
{
GetComponent<Light>().enabled = false;
}
}
if (battery < 0)
{
battery = 0;
GetComponent<Light>().enabled = false;
}
}
}
Attach that to you spotlight, and that should get you flashlight working. You're going to need a second script to get the replacement batteries working since the first script is attached to the light, and collisions won't work for it. So attach the second script to your player character. Now you just need to write a script to detect to detect the collision, destroy the battery, and tell the other script to increase the battery variable. Check out this : https://unity3d.com/learn/tutorials/modules/beginner/physics/on-collision-enter and this: http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html to help you.
Your answer
Follow this Question
Related Questions
How do I make a working flashlight with batteries? 0 Answers
Error : Unknown identifier : FlashLight 1 Answer
Flashlight with Partical help 0 Answers
Horror game project. 0 Answers
Need help using coroutines 1 Answer