- Home /
Battery Script If someone know the asnwer please help !!
I tried to make a Flashlight Battery Script but it's dont work. Can someone help me? -BATTERY SCRIPT:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Baterias : MonoBehaviour { public Flashlight flashlight;
[SerializeField] public int batteries = 2;
public void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
GameObject.FindGameObjectWithTag("Flashlight").GetComponent<Flashlight>().batteries = 2;
flashlight.batteries += 2;
Destroy(this.gameObject);
}
}
}
-FLASHLIGHT SCIRPT:
using System.Collections; using System.Text; using TMPro; using UnityEngine; using UnityEngine.UI;
[RequireComponent(typeof(Light))] [RequireComponent(typeof(AudioSource))] public class Flashlight : MonoBehaviour { #region Parameters
[SerializeField] KeyCode reloadKey = KeyCode.R;
[SerializeField] KeyCode toggleKey = KeyCode.F;
[SerializeField] int maxBatteries = 4;
[SerializeField] public int batteries = 2;
[SerializeField] bool autoReduce = true;
[SerializeField] float reduceSpeed = 1.0f;
[SerializeField] bool autoIncrease = false;
[SerializeField] float increaseSpeed = 0.5f;
[Range(0, 1)]
[SerializeField] float toggleOnWaitPrecentage = 0.05f;
public const float minBatteryLife = 0.0f;
[SerializeField] float maxBatteryLife = 10.0f;
[SerializeField] float followSpeed = 5.0f;
[SerializeField] Quaternion offset = Quaternion.identity;
#endregion
#region References
[SerializeField] AudioClip onSound = null;
[SerializeField] AudioClip offSound = null;
[SerializeField] AudioClip reloadSound = null;
[SerializeField] Image stateIcon = null;
[SerializeField] Slider lifeSlider = null;
[SerializeField] Image lifeSliderFill = null;
[SerializeField] TextMeshProUGUI reloadText = null;
[SerializeField] TextMeshProUGUI countText = null;
[SerializeField] CanvasGroup holder = null;
[SerializeField] Color fullLifeColor = Color.green;
[SerializeField] Color outLifeColor = Color.red;
[SerializeField] new Camera camera = null;
[SerializeField] GameObject flashlight = null;
#endregion
#region Statistics
[SerializeField]
private float batteryLife = 0.0f;
[SerializeField]
private bool usingFlashlight = false;
[SerializeField]
private bool outOfBattery = false;
#endregion
#region Private and Properties
private IEnumerator IE_UpdateBatteryLife = null;
Light _light = null;
Light Light
{
get
{
if (_light == null)
{
_light = GetComponent<Light>();
if (_light == null) { _light = gameObject.AddComponent<Light>(); }
_light.type = LightType.Spot;
}
return _light;
}
}
float defaultIntensity = 0.0f;
AudioSource _source = null;
AudioSource Source
{
get
{
if (_source == null)
{
_source = GetComponent<AudioSource>();
if (_source == null) { _source = gameObject.AddComponent<AudioSource>(); }
_source.playOnAwake = false;
}
return _source;
}
}
float GetLifePercentage
{
get
{
return batteryLife / maxBatteryLife;
}
}
float GetLightIntensity
{
get
{
return defaultIntensity * GetLifePercentage;
}
}
bool CanReload
{
get
{
return usingFlashlight && (batteries > 0 && batteryLife < maxBatteryLife);
}
}
bool MoreThanNeededPercetage
{
get
{
return GetLifePercentage >= toggleOnWaitPrecentage;
}
}
#endregion
private void Start()
{
Init();
}
private void Update()
{
if (Input.GetKeyDown(toggleKey))
{
ToggleFlashlight(!usingFlashlight, true);
}
if (Input.GetKeyDown(reloadKey) && CanReload)
{
Reload();
}
if (usingFlashlight)
{
transform.localRotation = Quaternion.Slerp(transform.localRotation, camera.transform.localRotation * offset, followSpeed * Time.deltaTime);
flashlight.transform.rotation = transform.rotation;
}
}
private void Reload()
{
batteryLife = maxBatteryLife;
Light.intensity = GetLightIntensity;
batteries--;
UpdateCountText();
UpdateSlider();
UpdateBatteryState(false);
UpdateBatteryLifeProcess();
PlaySFX(reloadSound);
}
void ToggleFlashlight(bool state, bool playSound)
{
usingFlashlight = state;
flashlight.SetActive(state);
state = (outOfBattery && usingFlashlight) ? false : usingFlashlight;
ToggleObject(state);
if (holder)
{
switch (usingFlashlight)
{
case true:
holder.alpha = 1.0f;
holder.blocksRaycasts = true;
break;
case false:
holder.alpha = 0.0f;
holder.blocksRaycasts = false;
break;
}
}
if (playSound)
{
PlaySFX(usingFlashlight ? onSound : offSound);
}
UpdateBatteryLifeProcess();
}
private void UpdateBatteryLifeProcess()
{
if (IE_UpdateBatteryLife != null) { StopCoroutine(IE_UpdateBatteryLife); }
if (usingFlashlight && !outOfBattery)
{
if (autoReduce)
{
IE_UpdateBatteryLife = ReduceBattery();
StartCoroutine(IE_UpdateBatteryLife);
}
return;
}
if (autoIncrease)
{
IE_UpdateBatteryLife = IncreaseBattery();
StartCoroutine(IE_UpdateBatteryLife);
}
}
private IEnumerator IncreaseBattery()
{
while (batteryLife < maxBatteryLife)
{
var newValue = batteryLife + increaseSpeed * Time.deltaTime;
batteryLife = Mathf.Clamp(newValue, minBatteryLife, maxBatteryLife);
Light.intensity = GetLightIntensity;
BatteryLifeCheck();
UpdateSlider();
yield return null;
}
}
private void BatteryLifeCheck()
{
if (MoreThanNeededPercetage && outOfBattery)
{
UpdateBatteryState(false);
UpdateBatteryLifeProcess();
}
}
private IEnumerator ReduceBattery()
{
while (batteryLife > 0.0f)
{
var newValue = batteryLife - reduceSpeed * Time.deltaTime;
batteryLife = Mathf.Clamp(newValue, minBatteryLife, maxBatteryLife);
Light.intensity = GetLightIntensity;
UpdateSlider();
yield return null;
}
UpdateBatteryState(true);
UpdateBatteryLifeProcess();
}
private void UpdateBatteryState(bool isDead)
{
outOfBattery = isDead;
if (reloadText)
{
reloadText.enabled = isDead;
}
if (stateIcon) { stateIcon.color = isDead ? new Color(1, 1, 1.5f) : Color.white; }
var state = outOfBattery ? false : usingFlashlight;
ToggleObject(state);
}
private void PlaySFX(AudioClip clip)
{
if (clip == null) { return; }
Source.clip = clip;
Source.Play();
}
void ToggleObject(bool state)
{
Light.enabled = state;
}
void UpdateSlider()
{
if (lifeSlider) { lifeSlider.value = batteryLife; }
if (lifeSliderFill)
{
lifeSliderFill.color = Color.Lerp(outLifeColor, fullLifeColor, GetLifePercentage);
}
}
void UpdateCountText()
{
if (countText)
{
StringBuilder countString = new StringBuilder("Batteries: ");
countString.Append(batteries);
countString.Append(" / ");
countString.Append(maxBatteries);
countText.text = countString.ToString();
}
}
private void Init()
{
defaultIntensity = Light.intensity;
batteryLife = maxBatteryLife;
UpdateBatteryState(false);
ToggleFlashlight(false, false);
UpdateCountText();
lifeSlider.minValue = minBatteryLife;
lifeSlider.maxValue = maxBatteryLife;
lifeSlider.value = batteryLife;
UpdateSlider();
if (!camera)
{
camera = Camera.main;
}
reloadText.text = "RELOAD(" + reloadKey + ")";
}
}
Answer by BrokeLol · Nov 27, 2021 at 09:33 AM
Check if your flashlight has a box collider/sphere collider and then tick the "isTrigger" box. It should probably work but If it doesn't have any collider, go to add component search "box collider" if it is cube shaped or "sphere collider" if it is sphere shaped. If this doesn't work see if your player has the tag you have written
THANK YOU!! Thanks for everybody i fixed it.. In battery script i change to this: public void OnTriggerEnter(Collider other) { if (other.CompareTag("Capsule")) { other.GetComponentInChildren().batteries += 2; flashlight.batteries += 1; Destroy(this.gameObject); } }
And i added a "UpdateCountText();" to "private void Update()" in Flaslight. Also i added a box collider to the flashlight and set it "trigger". And last i added a "capsule" tag to player.
Anyways thanks for everybody.
Answer by JethRow · Nov 26, 2021 at 02:41 AM
@pwolf1897 , can you try this out?
public void OnTriggerEnter(Collider other)
{
if (other.transform.tag=="Player")
{
other.GetComponent<Flashlight>().batteries += 2;
Destroy(this.gameObject);
}
}
But in Case that Flashlight script is in child of object that has collider just set
other.GetComponentInParent<Flashlight>().batteries += 2;
Or if Flashlight script is a child of your object that has collider that triggers it then set
other.GetComponentInChildren<Flashlight>().batteries += 2;
Also this will only work if ur collider that triggers the battery collider is on the same object as the flashlight Script,and check if the collider on the battery is set as trigger! Also remember to check if collider on your player is tagged as "Player" this must be exactly same!
Answer by pwolf1897 · Nov 26, 2021 at 09:04 PM
@JethRow It's doesn't work. :( I Upload some ScreenShots maybe it can help. Here's more screenshots: https://fastupload.co/1110904