-1

I am creating a game in unity and have run into a problem when resetting the speed to 0.

The game uses multiple throttle levels: -2 -1 0 1 2. When set to a throttle level the object is meant to speed up to a set speed. This works but when I come to reset it to 0 the speed sets its self to 0.0999999 even though 0 is set.

How can I fix this?

using UnityEngine;
using System.Collections;
using System;

public class Script_Control : MonoBehaviour {
public static float speedLvL;
public static float speed;
public static float health;
public static float manoverablity;
public static float tarSpeed;
public static float curSpeed;           
// Use this for initialization
void Start () {

//setting per ship stats
speedLvL = 0F;
speed = .25F;
health = 25F;
manoverablity = .25F;
//General Stats
tarSpeed = 0F;
curSpeed = 0F;

}

// FixedUpdate is called once per Time
void FixedUpdate () {
    Debug.Log(curSpeed);
//setting speed 
if (speedLvL == 0F){
    tarSpeed = 0.00000F;
}
if (speedLvL == 1F)  {
    tarSpeed = speed/2F;
}
if (speedLvL == 2F)  {
    tarSpeed = speed;
}
if (speedLvL == -1F)  {
    tarSpeed = -speed/5F;
}
if (speedLvL == -2F)  {
    tarSpeed = -speed/2.5F;
}

if (curSpeed < tarSpeed){
    curSpeed += .1F;
}
if (curSpeed > tarSpeed){
    curSpeed -= .1F;
}
    transform.Translate(curSpeed, 0, 0);

}
void Update(){

    if (Input.GetAxis("Throttle") > 0 && speedLvL <= 1 &&  speedLvL >= -2){
        speedLvL = speedLvL+1;      
        DateTime t = DateTime.Now; 
        DateTime tf = DateTime.Now.AddSeconds(.25);
        while (t < tf)
        {
            t = DateTime.Now;
        } 
    }   

    if (Input.GetAxis("Throttle") < 0 && speedLvL <= 2 &&  speedLvL >= -1){
        speedLvL = speedLvL-1;
        DateTime t = DateTime.Now; 
        DateTime tf = DateTime.Now.AddSeconds(.25);
        while (t < tf)
        {
            t = DateTime.Now;
        }
    }

    if (Input.GetAxis("Stearing") < 0){
        transform.Rotate(Vector3.forward* Time.deltaTime, manoverablity );

    }
    if (Input.GetAxis("Stearing") > 0){
        transform.Rotate(Vector3.forward* Time.deltaTime, -manoverablity);

    }
}



}

`

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
SteamPunk_Devil
  • 169
  • 1
  • 8

2 Answers2

1

In the FixedUpdate function you set the if statement which resets the speed to 0,

if (speedLvL == 0F){
    tarSpeed = 0.00000F;
}

however in the same function you have below the functions to either speed up or slow down the ship.

if (curSpeed < tarSpeed){
    curSpeed += .1F;
}

Looks like to me when you reset the ship to 0 it then adds the .1F on before it stops adjusting

You may need to split that function into 2 separate parts!

Hope this helps

WongKongPhooey
  • 398
  • 1
  • 5
  • 20
0

You cannot perform exact comparisons against floating point values (float or double) because they have inherent limits to the precision of the value they represent.

When you need to compare floating point values, you must always use a range (above and below) around the target value that you're trying to compare to - this way you can account for the imprecision of floating point storage.

Read this question / answer for more detail: Is it safe to check floating point values for equality to 0?

Community
  • 1
  • 1
xxbbcc
  • 16,930
  • 5
  • 50
  • 83