- Home /
Need help with a script to Pick up Batteries for a Flashlight type Lighter
I want the Player to be able to press "e" and pick up Lighter Fuel to expand the Lifespan of the Light. I have everything mostly set up, however I can't seem to get the Player to pick up the Fuel (even if it's On Collide only) The fuel Object doesn't do anything ingame Here is the script for the Lighter, it is attatched to the First Person Controller. The First Person Controller is tagged as "Player", it seems to work as it should.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class MatchesController : MonoBehaviour
{
public Light light;
public float PowerLevel = 1500.0f; //Fuel for Lighter
public AudioClip clickSound;
public AudioClip clickSound2;
void Start ()
{
light.enabled = false; //Disables the Light by default
}
void Update ()
{
if(Input.GetKeyDown(KeyCode.F)) //If the F key is pushed
{
if(light.enabled == false){ //Check if the Light is disabled, if it is, enable it and play a sound
light.enabled = true;
audio.PlayOneShot(clickSound);
}
else //Else, disable to light and play another sound
{
light.enabled = false;
audio.PlayOneShot(clickSound2);
}
}
if(light.enabled == true) { //decrease the Powerlevel if the light is on
PowerLevel -= 1.0f;
}
if (PowerLevel <= 500) { // decrease the light intensity if the PowerLevel falls beneath 500
light.intensity -= 0.0015f;
}
if (PowerLevel >= 501) { //if the Powerlevel is higher than 500, restore Intensity.
light.intensity = 0.8f;
}
if (PowerLevel <= 0) { //If the Powerlevel reaches Zero, disable the light and
light.enabled = false; //set the PowerLevel to Zero
PowerLevel = 0;
}
}
}
here is the script for The Fuel Object made by Olgo and modified so it works for my game, there is no press "e" option in here yet. It si atatched to the game object I want to use as the fuel that you can pick up for a lighter
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(SphereCollider))]
public class Battery : MonoBehaviour {
void Start(){
//get the Sphere Collider component of this Battery and make it a trigger.
this.GetComponent<SphereCollider>().isTrigger = true;
}
//This function will run whenever another object with a collider encounters this object's collider.
void OnTriggerEnter(Collider other){
//Check to see of the GameObject that collides with this Battery is tagged as Player
//For this to work, you will need to set your controllable GameObject to Player
if(other.gameObject.tag == "Player") {
other.GetComponent<MatchesController>().PowerLevel += 510.0f; //increase the Powerlevel by 510
}
}
}
I have tried a couple of hours now and I can't seem to find the error. I'm thankful for every help :)
yeah, but that option is not that important, what's more important is, that for some reason the collider in the "Battery" script doesn't seem to collide with the rigidbody of the Player for some reason
I followed what you said and afterward I could walk over the object and get the desired outcome, but that isn't exactly what I wanted because I need the player to enter the collider at a certain range. After Recreating the object as a new Prefab, it worked! Thank you so much :). could you repost your comment as answer so I can check it as answered :)
Answer by Cherno · May 19, 2014 at 01:49 AM
Your battery object needs a collider set to Is Trigger = true, you player Object needs a collider and a rigidbody. Also, check you Physics Preferences if there are any layers involved that don't interact in the Physics matrix.
Try
if(other.gameObject.CompareTag("Player")) {
instead of
if(other.gameObject.tag == "Player") {
Also, comment-out the tag condition completely to see if any collision registers at all.
Lastly, add Debug.Log lines at each and every step to see what works and what doesn't.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Battery life after the game ends PROBLEM 1 Answer
Battery Pickup Regeneration 2 Answers
Need Help Programming... Please Help! Thanks :) 1 Answer
Adding 1 point after collison 1 Answer