if 语句未在 c# unity 中执行

Ani*_*rma -2 c# unity-game-engine game-physics

我试图让一个角色移动一定距离,然后在他们移动一定距离时改变方向......我的代码中的逻辑可能是错误的我仍然需要解决这不是问题,问题是我的 if 语句不执行

public class EnemyControl : MonoBehaviour
{
    private int xMoveDirection=-1;
    private float x;
    void Start()
    {
        x=gameObject.transform.position.x;
    }
    void Update()
    {
        gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(xMoveDirection,0);
        if(x==0.00){
          Debug.Log("helloimhere");
          xMoveDirection=0;
          x=x+1;
        }
Run Code Online (Sandbox Code Playgroud)

Bli*_*ndy 6

任何与浮点值的直接比较很可能会失败,你最好定义一个“足够接近”的值范围,例如Math.Abs(x) < 0.0001