Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Help with fuel slider decreasing over time, it should freeze and decrease when controls are pushed.

Discussion in 'Scripting' started by unity_EJvhBoVVDwSiWg, Jun 14, 2022.

  1. unity_EJvhBoVVDwSiWg

    unity_EJvhBoVVDwSiWg

    Joined:
    Jun 14, 2022
    Posts:
    5
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     public Rigidbody rb;
    9.     public ParticleSystem MainThruster;
    10.     public ParticleSystem LeftThruster;
    11.     public ParticleSystem RightThruster;
    12.     public Slider fuelBar;
    13.  
    14.     public float fuel = 1.0f;
    15.  
    16.     void UseFuel()
    17.     {
    18.         fuelBar.value -= 0.05f;
    19.         if (fuel < 0) fuel = 0f;
    20.     }
    21.  
    22.     private void FixedUpdate()
    23.     {
    24.         if (Input.GetAxis("Vertical") > 0.01f)
    25.         {
    26.             rb.AddForce(transform.up * 150.0f);
    27.             UseFuel();
    28.        
    29.  
    30.             if (!MainThruster.isPlaying)
    31.                 MainThruster.Play();
    32.             else if (MainThruster.isPlaying)
    33.                 MainThruster.Stop();
    34.         }
    35.  
    36.      
    37.         if (Input.GetAxis("Horizontal") > 0.01f || (Input.GetAxis("Horizontal") < -0.01f)) ;
    38.         {
    39.             rb.AddForce((transform.right * 150.0f) * Input.GetAxis("Horizontal"));
    40.  
    41.             UseFuel();
    42.  
    43.             if (!RightThruster.isPlaying && Input.GetAxis("Horizontal") < -0.01f)
    44.          
    45.                 RightThruster.Play();
    46.          
    47.             else if (!LeftThruster.isPlaying && Input.GetAxis("Horizontal") > 0.01f)
    48.          
    49.                 LeftThruster.Play();
    50.          
    51.         }
    52.         {
    53.  
    54.             if (RightThruster.isPlaying && Input.GetAxis("Horizontal") < 0.01f)
    55.  
    56.  
    57.                 RightThruster.Stop();
    58.  
    59.  
    60.  
    61.             if (LeftThruster.isPlaying && Input.GetAxis("Horizontal") > -0.01f)
    62.  
    63.                 LeftThruster.Stop();
    64.         }
    65.      
    66.     }
    67. }
    Hey!

    I am pretty new to scripting and im having a school assignment, that needs a fuel bar for the spacecraft. I want it to decrease when I move the spacecraft, and it does. But it also decreases no matter if I push something or not. It decreases more when I push. I want it to "freeze" when I dont do anything, like standing still or just free falling. Could anyone help? Thanks!!
     
    Last edited: Jun 14, 2022
  2. unity_EJvhBoVVDwSiWg

    unity_EJvhBoVVDwSiWg

    Joined:
    Jun 14, 2022
    Posts:
    5
    Also if could help with adding so that the ship can't move when the fuel bar is empty.
     
  3. unity_EJvhBoVVDwSiWg

    unity_EJvhBoVVDwSiWg

    Joined:
    Jun 14, 2022
    Posts:
    5
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Have you at least eliminated all the compiler errors from your project?

    For example, we know this code doesn't even compile:

    Screen Shot 2022-06-14 at 12.07.54 PM.png

    Because you are subtracting 0.05f multiplied by nothing.

    Make sure your log console selector buttons are enabled. See this graphic:

    https://forum.unity.com/threads/cant-add-script-component.632746/#post-4239121

    https://forum.unity.com/threads/update-function-not-working.953477/#post-6215873

    You SHOULD be seeing red squiggly line errors in your IDE.

    If you are not...

    This may help you with intellisense and possibly other Visual Studio integration problems:

    Sometimes the fix is as simple as doing Assets -> Open C# Project from Unity. Other times it requires more.

    Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files that Visual Studio tends to slowly damage over time, then try the above trick.

    Barring all that, move on to other ideas:

    https://forum.unity.com/threads/intellisense-not-working-with-visual-studio-fix.836599/

    Also, try update the VSCode package inside of Unity: Window -> Package Manager -> Search for Visual Studio Code Editor -> Press the Update button

    Also, this: https://forum.unity.com/threads/no-suggestions-in-vscode.955197/#post-6227874
     
    unity_EJvhBoVVDwSiWg likes this.
  5. unity_EJvhBoVVDwSiWg

    unity_EJvhBoVVDwSiWg

    Joined:
    Jun 14, 2022
    Posts:
    5
    Sorry, I have removed the mark. I did that after copying the code... but out of frustration that I couldn't find anything on google etc, I kindoff forgot to fix that when I posted it here. So the code works, but the slider decreases over time + when inputs are down. I want only to decrease when inputs are clicked. Sorry again. New to all of this. But thank you for awesome reply!
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code you think is executing is executing when it should not
    l- the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? why? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    You must find a way to get the information you need in order to reason about what the problem is.
     
  7. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    Remove the semi-colon from the end of your if statement on line 37. What that is doing is actually ending the if condition, and therefore every line that you think is encapsulated in the if statement is being run regardless (ie: lines 38 to 51 are being run as they are not counted as being part of the if statement block because of the ; on line 37).
     
  8. unity_EJvhBoVVDwSiWg

    unity_EJvhBoVVDwSiWg

    Joined:
    Jun 14, 2022
    Posts:
    5

    Omg, thank you so much!! It fixed it :D!! THANKS.


    Thank you for this also!! Appreciate it. :D