Hello Good Day. i am wondering how can i make my fuel bar not refuel instantly every time it touches the ground. what i want to make it happen is when i release the Jump Key or a certain Key for going up. it will slowly refuel. here in this code it just refuel instantly when it touches the ground. i tried Sir Helium’s Suggestion about the Co routine but i cannot make it work and still confused “Beginner”. i hope anyone can help me out . Advance thanks and God bless.
public static float jetPackFuel = 4f;
public float jetPackForce = 15.0f;
public GameObject jetPack;
private bool isGrounded = true;
// Update is called once per frame
void Update () {
if (Input.GetButton ("Jump") && jetPackFuel >= 0.001f) {
BoostUp ();
jetPack.GetComponent<ParticleSystem> ().Play ();
} else {
jetPack.GetComponent<ParticleSystem> ().Stop ();
}
}
void BoostUp () {
isGrounded = false;
jetPackFuel = Mathf.MoveTowards (jetPackFuel, 0, Time.fixedDeltaTime);
GetComponent<Rigidbody> ().AddForce (new Vector3 (0, jetPackForce));
}
void OnCollisionEnter (Collision Col){
if (Col.gameObject.tag == "Ground") {
isGrounded = true;
//CREATE METHOD HERE
Refuel();
}
}
void Refuel () {
//Originally, this is only getting called once because OnCollisionEnter is only getting called once When you hit the ground
//so to fix that we can use a while loop
if (isGrounded == true){
while (jetPackFuel < 2.9f){
jetPackFuel = Mathf.MoveTowards (jetPackFuel, 3, 0.001f * Time.fixedDeltaTime);
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Player_Controls : MonoBehaviour
{
/// <summary>
/// The max fuel of the jetpack.
/// </summary>
public float MaxJetPackFuel = 4f;
/// <summary>
/// The speed of the jetpack.
/// </summary>
public float jetPackForce = 15.0f;
/// <summary>
/// The fuel cost per second.
/// </summary>
public float jetPackFuelCost = 0.25f;
/// <summary>
/// The fuel regen per second.
/// </summary>
public float jetPackFuelRegen = 0.15f;
// The current amount of fuel.
private float currentFuel;
/// <summary>
/// The Jetpack gameobject.
/// </summary>
public GameObject jetPack;
/// <summary>
/// The UI image for the fuel bar.
/// </summary>
public Image fuelBar;
// Is the gameobject grounded?
private bool isGrounded = true;
// The particle system of the jetpack.
private ParticleSystem jetPackParticle;
// The rigidbody attached to the gameobject.
private Rigidbody body;
// OnEnable is called before Update, each time the object is enabled or loaded.
void OnEnabie()
{
// Find and set our particle system.
jetPackParticle = jetPack.GetComponent<ParticleSystem>();
// Find and set our Rigidybody.
body = GetComponent<Rigidbody>();
// Set our initial fuel.
currentFuel = MaxJetPackFuel;
// Configure out fuel bar.
fuelBar.type = Image.Type.Filled;
fuelBar.fillMethod = Image.FillMethod.Horizontal;
fuelBar.fillAmount = 1.0f;
}
// Update is called many times per second.
void Update()
{
// If our player is pressing the jump button.
if (Input.GetButton("Jump"))
{
// If our fuel is not empty.
if (currentFuel > 0)
{
// Call our boost method.
BoostUp();
// Play our particle system.
jetPackParticle.Play();
}
}
// If the player is grounded and fuel is not max.
else if (isGrounded && currentFuel < MaxJetPackFuel)
{
// Call our refuel method.
Refuel();
}
}
void BoostUp()
{
// If the player is on the ground.
isGrounded = false;
// Reduce the current fuel.
currentFuel -= jetPackFuelCost * Time.deltaTime;
// Add upwards velocity.
body.velocity = Vector3.up * jetPackForce * Time.deltaTime;
// Update our fuel bar.
fuelBar.fillAmount = Mathf.InverseLerp(0, MaxJetPackFuel, currentFuel);
}
void OnCollisionEnter(Collision Col)
{
// If we touched the ground (usually there is a better way than to check for tags)
if (Col.gameObject.tag == "Ground")
{
// Set our grounded bool to true.
isGrounded = true;
// Stop our particles.
jetPackParticle.Stop();
}
}
void Refuel()
{
// If te player is on the ground.
if (isGrounded == true)
{
// If the fuel is not max.
if (currentFuel < MaxJetPackFuel)
{
// Increase the current fuel.
MaxJetPackFuel += jetPackFuelRegen * Time.deltaTime;
// Update our fuel bar.
fuelBar.fillAmount = Mathf.InverseLerp(0, MaxJetPackFuel, currentFuel);
}
}
}
}
I optimized some of the code you were using, and notated it, I hope this help.