- Home /
Battery Pickup Regeneration
I'm trying to make it so that whenever my player collides with a battery on the floor it dissapears and then fills up my flashlight life, I have a script for my flashlight to make it drain over time and fade out but I have been trying to do this battery pickup for a while now and just cant get it to work, I've tried so many scripts and im not very good with unity which is why im learning, help would be very much appreciated, here is my flashlight script code with the Javascript name (Player_Toggle_Flashlight)
The problem that I get with most scripts I use is that my player will get the option to press 'E' over the betteries but when I press 'E' it will go off the 'game' back into 'scene' mode with my player stood over the battery with it under his feet, this keeps occuring, it will just make the unity go off the game scene and the battery wont destroy.
#pragma strict
//Variables START
//Light Source && Battery Life
var flashlightLightSource : Light;
var lightOn : boolean = true;
var lightDrain : float = 0.1;
var batteryLife : float = 0.0;
var maxBatteryLife : float = 2.0;
//Sound Clips
var soundTurnOn : AudioClip;
var soundTurnOff : AudioClip;
//Variables END
function Start()
{
batteryLife = maxBatteryLife;
flashlightLightSource = GetComponent(Light);
}
function Update()
{
if(lightOn && batteryLife >= 0)
{
batteryLife -= Time.deltaTime * lightDrain;
}
flashlightLightSource.light.intensity = batteryLife;
if(batteryLife <= 0)
{
batteryLife = 0;
lightOn = false;
}
if(Input.GetKeyUp(KeyCode.F))
{
toggleFlashlight();
toggleFlashlightSFX();
if(lightOn)
{
lightOn = false;
}
else if(!lightOn && batteryLife >= 0)
{
lightOn = true;
}
}
}
function toggleFlashlight()
{
if(lightOn)
{
flashlightLightSource.enabled = false;
}
else
{
flashlightLightSource.enabled = true;
}
}
function toggleFlashlightSFX()
{
if(flashlightLightSource.enabled)
{
audio.clip = soundTurnOn;
}
else
{
audio.clip = soundTurnOff;
}
audio.Play();
}
@script RequireComponent(AudioSource)
Answer by Cherno · May 16, 2014 at 12:40 AM
function OnTriggerEnter(other : Collider)
{
if(other.collider.transform.CompareTag("Battery") == true)
{
batteryLife = maxBatteryLife;
Destroy(other.transform.gameObject);
}
}
Now you only need a battery gameObject that has a collider (box collider should suffice) with it's tag set to Battery or whatever, and the collider set to Is Trigger = true. Also, the player object needs not only a collider but also a rigidbody.
it came up with 'unknown identifier maxBatteryLife' so i changed the script to
batteryLife = "maxBatteryLife";
but its not seem to be doing anything, iv added a cube with the mesh off and box collider on and tagged it to the battery object and put them both together, then i added a rigidbody onto the graphics of my character with the battery script dragged onto my battery, I have noticed that if i tick 'is trigger' on the battery it will walk straight through the object yet if it isn't ticked it will just make it solid
Line 10 of your original post shows a variable called maxBatteryLife
so it's a puzzle why you get an error message saying this doesn't exist. Is the spelling 101% correct? Also, changing the name of a variable into a string doesn't do what you think it does.
Answer by Adam_dh321 · May 16, 2014 at 02:54 PM
which tag do i add to my battery, I added a cube with box collider on and trigger, this is tagged as 'Battery', the battery object itself hasn't got a tag and is marked untagged (grouped cube and battery together)Ingame I have placed the cube over my battery object with the mesh renderer off but box collider and trigger on, the battery has mesh renderer on and mesh collider and trigger off
I have made a rigid body under my graphics which is linked to my player (First person controller) also, I know some of the tagging and stuff if probably wrong but I have been played around with the tags, colliders and various different scripts etc for 2 days and cant get it too work :S. Its still coming up 'unknown identifier maxBatteryLife' which is puzzling as the variable is definitely that :S
In the project menu at the bottom my battery is named 'Battery', does this make a difference? im talking about the object which shows up after you import it from Maya and when you click on it it comes up with 'generate colliders' etc
Please post comments or follow-up questions as comments, NOT as answers.
You can't just attach a seperate gameobject liek a cube to your battery object (you could, but the scripting would be different).
What you ahve to do is exactly what I have described in my answer: The battery object ITSELF needs the collider and tag. What the exact name of the gameobject is doesn't matter.
It wont let me resume the scene because of the unknown identifer 'maxBatteryLife' :S, do you know what the problem is with that because that is deffinatly the variable on my flashlight script
No, I don't know what the problem is with that, as Graham Dunnett already wrote since you declared it, you should be able to use it. Just to be clear: Atthe top of your script, you have to have the veriable declaration var maxBatteryLife : float = 2.0;, and below in the function you just change the value of the maxBatteryLife variable.
In any case, change both occurences of the variable (declaration and value changing) to any other term, like battery$$anonymous$$axLife or something.
just tried messing with the value and declaration by changing the names on both etc, was thinking, im not very good with knowing about coding but is there a way of doing it by using raycast and adding a certain amount ins$$anonymous$$d of putting it back up to max value, maybe this would get around the error :S I have another torch script which works good but cant seem to get the battery part of that working either, sorry for being a pain
I have figured out the problem with the not identified and It was something embarassing that I wont even say, after that it said I need to make 'batteryLife' static so I added static before the variable, it still seems to not be doing anything though, is adding the rigidbody onto my graphics right and adding a component onto the battery etc
is it because the script is initially attached to my lightsource spotlight above the characters head?, I tried putting that on my graphics but it wont allow me