简体   繁体   English

为什么位置在Unity3D中不起作用?

[英]Why if position is not working in Unity3D?

I have a problem with checking if my gameobject is on a position, let me explain: I'm making a game with a kind of "Slider", like the Unity UI Slider, (But not using the slider, it's just to compare), this Slider was made with GameObjects. 我在检查我的游戏对象是否在某个位置时遇到问题,请让我解释一下:我正在制作一种带有“滑块”的游戏,例如Unity UI滑块,(但不使用滑块,这只是为了进行比较) ,此Slider是使用GameObjects制作的。 The cursor was designed to move automatically and you press the screen when the cursor is in a specific zone. 光标被设计为自动移动,并且当光标在特定区域中时您可以按屏幕。 This is the cursor code: 这是游标代码:

 private System.Random FirstRnd = new System.Random();
     private int FirstCounter;
     private float Speed = 50f;
     private float AntiSpeed = -50f;
     private int SecondCounter;
     private System.Random SecondRnd = new System.Random();
     private Vector3 StartLVector = new Vector3(-2.05f, -0.55f, 10f);
     private Vector3 StartRVector = new Vector2(2.05f, -0.55f);
     private Vector3 CenterL = new Vector3(-0.02f, 0.55f, 10f);
     private Vector3 CenterR = new Vector3(1.14f, -0.55f, 10f);
     private GameObject Cursor;

     // Use this for initialization
     void Start ()
     {
         Cursor = GameObject.Find("Cursor");
     }
     private void FixedUpdate()
     {
         SecondStarting();
     }
     void Starting()
     {
         FirstCounter = FirstRnd.Next(1, 3);
         if (FirstCounter == 1)
         {
             transform.position = StartLVector;
         }
         else if (FirstCounter == 2)
         {
             transform.position = StartRVector;
         }
     }
     void SecondStarting()
     {
         Debug.Log("Puga");
         if (Cursor.transform.position == StartLVector)
         {
             Cursor.transform.Translate(Speed, 0, 0);
         }
         else if (Cursor.transform.position == StartRVector)
         {
             Cursor.transform.Translate(AntiSpeed, 0, 0);
         }
         else if (Cursor.transform.position == CenterL)
         {
             SecondCounter = SecondRnd.Next(1, 3);
             if (SecondCounter == 1)
             {
                 Cursor.transform.Translate(Speed, 0, 0);
             }
             else if (SecondCounter == 2)
             {
                 Cursor.transform.Translate(AntiSpeed, 0, 0);
             }
         }
         else if (Cursor.transform.position == CenterR)
         {
             SecondCounter = SecondRnd.Next(1, 3);
             if (SecondCounter == 1)
             {
                 Cursor.transform.Translate(Speed, 0, 0);
             }
             else if (SecondCounter == 2)
             {
                 Cursor.transform.Translate(AntiSpeed, 0, 0);
             }
         }
     }
 }

As far I can see, I don't detect a problem here. 据我所知,这里没有发现问题。 Do you see something? 看到什么了吗 I want to add something, the message "Puga" yes prints, but, below it, nothing seems to compile 我想添加一些内容,打印出消息“ Puga”,但在其下方似乎没有任何编译

Comparisons by floats are inherently inaccurate and will often return false even when they appear equal. 浮点数比较本质上是不准确的,即使它们看起来相等,也经常会返回false。 A vector 3 consists of three floats (xyz). 向量3由三个浮点数(xyz)组成。 Even though unity's == operator for vector3 compares the square magnitude, You still have a pretty large margin for error. 即使vector3的单位==运算符比较了平方幅度,您仍然有相当大的误差余量。

An Alternative could be that you try and round the xyz properties (or xy for vector2) to one or two decimals and the compare them. 另一种选择是,您尝试将xyz属性(或vector2的xy)四舍五入为一个或两个小数,然后进行比较。

System.Math.Round(value, 2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM