Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm a beginner learning to code with Unity as well. I want to set up a Fuel System so that when the car moves the Gas is used up. But my UI slider (that shows the fuel gauge) just doesn't seem to be working. Please help on the below 2 scripts:

Everyone please don't mind the code I'm trying to learn all this from every video I can. I just can't seem to find a book to help learn C# and Unity together. I know most of the basics but still.

Please help!! Thank you so much...

What I have tried:

public class PlayerController : MonoBehaviour
{
    private Rigidbody2D rb2d;
    private int count;

    public float speed;
    public Text countText;

    FuelSystem fuelSystem;
    
    // Start is called before the first frame update
    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
        SetCountText();

        fuelSystem = GetComponent<FuelSystem>(); 
    }

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        Vector2 movement = new Vector2(moveHorizontal, moveVertical);

        rb2d.AddForce(movement * speed);

        fuelSystem.fuelComsumptionRate = speed;
        fuelSystem.ReduceFuel();

        if (Input.GetButtonDown("Jump"))
        {
            gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 2.5f), ForceMode2D.Impulse);
        }
    }


    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag ("Coin"))
        {
            other.gameObject.SetActive(false);
            count = count + 1;
            SetCountText();
        }
    }

    void SetCountText()
    {
        countText.text = "Coins " + count.ToString() + " of 3";
    }
}


public class FuelSystem : MonoBehaviour
{
    public float startFuel;
    public float maxFuel;
    public float fuelComsumptionRate;


    public Slider fuelIndicatorSld;
    public Text fuelIndicatorTxt;

    void Start()
    {
        if (startFuel > maxFuel)
        {
            startFuel = maxFuel;

            fuelIndicatorSld.maxValue = maxFuel;
            UpdateUI();

        }
    }


    public void ReduceFuel ()
    {
        startFuel = Time.deltaTime * fuelComsumptionRate;
        UpdateUI();
    }

    void UpdateUI()
    {
        fuelIndicatorSld.value = startFuel;
        fuelIndicatorTxt.text = "Fuel Left:" + startFuel.ToString("0") + " %";

        if (startFuel <= 0)
        {
            startFuel = 0;
            fuelIndicatorTxt.text = "Out of Gas!";
        }
    }
}
Posted
Updated 28-May-20 7:17am

1 solution

Does the text output reflect what you would expect? Your fuel system code only sets the max value of the slider if startFuel > maxFuel, are you setting the values in the editor for when that condition isn't true? Also in ReduceFuel you set startFuel to be the deltaTime * rate. If you are going at a constant speed with a constant frame rate then you're just setting startFuel to be the same value so you'll see no, or little change. You probably mean

C#
startFuel -= Time.deltaTime * fuelComsumptionRate;


The "-=" is shorthand for

C#
startFuel = startFuel - (...)


so you are subtracting the fuel rate from the fuel value which will see your fuel drop in relation to the speed.
 
Share this answer
 
Comments
CodingGreek 29-May-20 1:48am    
Yes. Thank you!! The fuel slider does go down. But if you could help me in just one more thing. Though the car is in one place the fuel gauge keeps going down to zero and also the car still keeps moving. Like you said on the above comment how would I be able to configure this on my script?
F-ES Sitecore 29-May-20 7:00am    
You're setting the fuel rate to be the speed, which looks like it might be a constant, I don't see you changing it. If the speed stays the same the fuel rate stays the same so your fuel is used at a constant rate regardless. If your game is side-scrolling I don't know why you're adding vertical force based on axis? Regardless, what you need to do is set fuelComsumptionRate to be something based on the current inputs, something like

moveHorizontal * speed

That way if there is no horizontal input you use no fuel, and the greater the horizontal input the more fuel you use. This is if the game is indeed side-scrolling, if not you'll need to use the magnitude of "movement" to factor in both inputs.

You'll also need to convert the moveHorizontal to positive if it is negative, otherwise you'll "gain" fuel going in reverse :)
CodingGreek 30-May-20 0:16am    
Yes it is a side scroller... I forgot that I had placed vertical force when I did not need it. I was using unity learning to learn the scripting .
Thank you for you help I will update it as such :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900