- Home /
problem with boost script
I have a boost script for my game that is suppose to act similar to the boost ability seen in racing games like burnout and hydro thunder where the player can hold down the boost button until the boost gauge depletes. I have basically replicated this but for some reason it only works in the unity editor, when running in the game build it only lasts for a slit second.
here's the script:
[RequireComponent(typeof(AudioSource))]
public class NitroPlayer1 : MonoBehaviour {
public GameObject leftflames;
public GameObject rightflames;
public AudioClip activateSound;
AudioSource audioSource;
public GameObject vehicle;
static float remainingNitrous = 30;
static float MaxNitroCapacity = 30;
public bool CanRefill = false;
public Rigidbody rb;
static float thrust = 50000;
// Use this for initialization
void Start () {
audioSource = GetComponent<AudioSource>();
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
if (Input.GetButton("Fire1")){
if(remainingNitrous > 0){
leftflames.gameObject.SetActive(true);
rightflames.gameObject.SetActive(true);
}
}
if (Input.GetButtonUp("Fire1")){
leftflames.gameObject.SetActive(false);
rightflames.gameObject.SetActive(false);
}
if (remainingNitrous == 0){
leftflames.gameObject.SetActive(false);
rightflames.gameObject.SetActive(false);
}
if (Input.GetButtonUp("Fire1")){
if (remainingNitrous > 0){
CanRefill = true;
}
}
if(Input.GetButtonUp("Fire1")){
if(remainingNitrous == 0){
CanRefill = true;
}
}
if (remainingNitrous == MaxNitroCapacity){
CanRefill = false;
}
if (CanRefill){
remainingNitrous +=1;
}
if (Input.GetButtonDown("Fire1")){
if (remainingNitrous > 0){
audioSource.PlayOneShot(activateSound, 0.7F);
}
}
if (Input.GetButton ("Fire1")){
if (remainingNitrous > 0){
Nitrous();
}
}
}
void Nitrous(){
rb.AddForce(transform.forward * thrust);
remainingNitrous -= 1;
}
}
the player's max boost capacity is 30.
Answer by hexagonius · Mar 24, 2018 at 11:17 AM
that's because you're removing 1 each frame. builds run faster than the editor. this means your 30 removals happen, you said it, within a split of a second. I'd use float values for the actual boost and remove Time.deltaTime, as is for 1 per second, or multiplied by a factor for as many units per second as you like. No matter what you show to the user.
ahh I see what you mean, when I subtract 1 point from the remaining nitro it happens every frame as you stated, frames must advance every fraction of a second on the builds. Which must be mainly useful for gun ammunition float variables
Your answer
Follow this Question
Related Questions
Float RigidBody2D whitout Move any place, static position -1 Answers
setting max varible value 5 Answers
Problems comparing float with a Mathf.RoundToInt() version of itself 1 Answer
How to i set the int to hitpoints / 10 + the current int. 1 Answer
Why is this Int lagging when its subtracting something over a period of time? 1 Answer