Unity检测左/右/上/下

huangapple go评论100阅读模式
英文:

Unity detect left/right/up/down

问题

I am implementing my code on an object and the player's hand is the origin to detect if it is in the direction of right/left/up/down.

So far the code is working to detect all 4 axes but there is a problem.
If the direction is left/right, it prints both "Left" and "Up," similarly "Right" and "Up." Can it be just "Left" or "Right" and not print the y-axis along with it?

How do I fix this?

  1. public GameObject playersHand;
  2. private Transform targetOrigin;
  3. private Vector2 targetPos;
  4. private Vector2 mypos;
  5. void Start()
  6. {
  7. targetOrigin = this.gameObject.transform;
  8. }
  9. void Update()
  10. {
  11. mypos = playersHand.transform.position;
  12. targetPos = targetOrigin.position;
  13. if (targetOrigin.position.x < mypos.x && changed(mypos.y, targetPos.y))
  14. {
  15. print("Right");
  16. }
  17. else if (changed(mypos.y, targetPos.y) && targetOrigin.position.x > mypos.x)
  18. {
  19. print("Left");
  20. }
  21. if (targetOrigin.position.y < mypos.y && changed(mypos.x, targetPos.x))
  22. {
  23. print("Up");
  24. }
  25. else if (changed(mypos.x, targetPos.x) && targetOrigin.position.y > mypos.y)
  26. {
  27. print("Down");
  28. }
  29. }
  30. bool changed(float a, float b)
  31. {
  32. return (int)a == (int)b;
  33. }

请注意,我已经根据您的要求将代码翻译成中文,不包括代码部分。

英文:

I am implementing my code on an object and the player's hand is the origin to detect if it is in the direction of right/left/up/down.

So far the code is working to detect all 4 axis but there is a problem.
If the direction is left/right, it prints both "Left" and "Up", similarly "Right" and "Up". Can it be just "Left" or "Right" and not print the y axis along with it?

How do I fix this?

  1. public GameObject playersHand;
  2. private Transform targetOrigin;
  3. private Vector2 targetPos;
  4. private Vector2 mypos;
  5. void Start()
  6. {
  7. targetOrigin = this.gameObject.transform;
  8. }
  9. void Update()
  10. {
  11. mypos = playersHand.transform.position;
  12. targetPos = targetOrigin.position;
  13. if (targetOrigin.position.x &lt; mypos.x &amp;&amp; changed(mypos.y, targetPos.y))
  14. {
  15. print (&quot;Right&quot;);
  16. }
  17. else if (changed(mypos.y, targetPos.y))
  18. {
  19. print (&quot;Left&quot;);
  20. }
  21. if (targetOrigin.position.y &lt; mypos.y &amp;&amp; changed(mypos.x, targetPos.x))
  22. {
  23. print (&quot;Up&quot;);
  24. }
  25. else if (changed(mypos.x, targetPos.x))
  26. {
  27. print (&quot;Down&quot;);
  28. }
  29. }
  30. bool changed(float a, float b)
  31. {
  32. if ((int)a == (int)b)
  33. {
  34. return true;
  35. }
  36. else
  37. {
  38. return false;
  39. }
  40. }

答案1

得分: 0

我认为问题出在逻辑上。
你需要检查位置是否在水平方向。
应该可以这样做:

  1. if (targetOrigin.position.x < mypos.x && Mathf.Abs(mypos.x - targetPos.x) > Mathf.Abs(mypos.y - targetPos.y))
  2. {
  3. print("右");
  4. }
  5. else if (Mathf.Abs(mypos.x - targetPos.x) > Mathf.Abs(mypos.y - targetPos.y))
  6. {
  7. print("左");
  8. }
  9. if (targetOrigin.position.y < mypos.y && Mathf.Abs(mypos.y - targetPos.y) > Mathf.Abs(mypos.x - targetPos.x))
  10. {
  11. print("上");
  12. }
  13. else if (Mathf.Abs(mypos.y - targetPos.y) > Mathf.Abs(mypos.x - targetPos.x))
  14. {
  15. print("下");
  16. }
英文:

I think the problem is logic.
you need to check if the position is in the horizontal direction.
that should work:

  1. if (targetOrigin.position.x &lt; mypos.x &amp;&amp; Mathf.Abs(mypos.x - targetPos.x) &gt; Mathf.Abs(mypos.y - targetPos.y))
  2. {
  3. print (&quot;Right&quot;);
  4. }
  5. else if (Mathf.Abs(mypos.x - targetPos.x) &gt; Mathf.Abs(mypos.y - targetPos.y))
  6. {
  7. print (&quot;Left&quot;);
  8. }
  9. if (targetOrigin.position.y &lt; mypos.y &amp;&amp; Mathf.Abs(mypos.y - targetPos.y) &gt; Mathf.Abs(mypos.x - targetPos.x))
  10. {
  11. print (&quot;Up&quot;);
  12. }
  13. else if (Mathf.Abs(mypos.y - targetPos.y) &gt; Mathf.Abs(mypos.x - targetPos.x))
  14. {
  15. print (&quot;Down&quot;);
  16. }

huangapple
  • 本文由 发表于 2023年5月10日 16:46:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定