To simplify, my player has a flashlight and after a set amount of time the battery percentage should lower by 1%. Since it starts at 100%, after some time -> 99% then 98% and so on. So far, I've tried using InvokeRepeating and coroutines but both have given me issues. Bellow is my current script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
public class Flashlight : MonoBehaviour
{
public Transform player;
public Light flashlight;
public Vector3 offset;
public float rotateSpeed;
public float threshold;
public float battery;
public Text batteryDisplay;
public float percentDropTime;
void start()
{
flashlight = GetComponent();
InvokeRepeating("LowerBattery", percentDropTime, 0);
}
void Update()
{
UnityEngine.Debug.Log(battery);
transform.position = player.position + offset;
transform.eulerAngles = new Vector3(player.eulerAngles.x, player.eulerAngles.y + rotateSpeed, player.eulerAngles.z);
UpdateBattery();
}
void UpdateBattery()
{
batteryDisplay.text = "Battery " + battery.ToString() + "%";
}
void LowerBattery()
{
battery--;
}
}
,
↧