我建立在统一游戏和复位速度为0。与浮点精度
游戏时所遇到的一个问题处理使用多个油门水平:-2 -1 0 1 2,当设置为该物体的油门水平意味着加速到设定的速度。这有效,但是当我将它重置为0时,即使设置了0,速度也会将其自身设置为0.0999999。
我该如何解决这个问题?
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);
}
}
}
`
谢谢多数民众赞成我太被卷入浮动不精确我不认为它可能是任何东西 – 2015-02-10 22:56:02