Unity检测左/右/上/下

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

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?

public GameObject playersHand;
private Transform targetOrigin;
private Vector2 targetPos;
private Vector2 mypos;

void Start()
{
    targetOrigin = this.gameObject.transform;
}

void Update()
{
    mypos = playersHand.transform.position;
    targetPos = targetOrigin.position;

    if (targetOrigin.position.x < mypos.x && changed(mypos.y, targetPos.y))
    {
        print("Right");
    }
    else if (changed(mypos.y, targetPos.y) && targetOrigin.position.x > mypos.x)
    {
        print("Left");
    }

    if (targetOrigin.position.y < mypos.y && changed(mypos.x, targetPos.x))
    {
        print("Up");
    }
    else if (changed(mypos.x, targetPos.x) && targetOrigin.position.y > mypos.y)
    {
        print("Down");
    }
}

bool changed(float a, float b)
{
    return (int)a == (int)b;
}

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

英文:

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?

public GameObject playersHand;
private Transform targetOrigin;
private Vector2 targetPos;
private Vector2 mypos;

void Start()
{
    targetOrigin = this.gameObject.transform;
}

void Update()
{
    mypos = playersHand.transform.position;
    targetPos = targetOrigin.position;

    if (targetOrigin.position.x &lt; mypos.x &amp;&amp; changed(mypos.y, targetPos.y))
    {
        print (&quot;Right&quot;);
    }
    else if (changed(mypos.y, targetPos.y))
    {
        print (&quot;Left&quot;);
    }

    if (targetOrigin.position.y &lt; mypos.y &amp;&amp; changed(mypos.x, targetPos.x))
    {
        print (&quot;Up&quot;);
    }
    else if (changed(mypos.x, targetPos.x))
    {
        print (&quot;Down&quot;);
    }
}

bool changed(float a, float b)
{
    if ((int)a == (int)b)
    {
        return true;
    }
    else
    {
        return false;
    }
}

答案1

得分: 0

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

if (targetOrigin.position.x < mypos.x && Mathf.Abs(mypos.x - targetPos.x) > Mathf.Abs(mypos.y - targetPos.y))
{
    print("右");
}
else if (Mathf.Abs(mypos.x - targetPos.x) > Mathf.Abs(mypos.y - targetPos.y))
{
    print("左");
}

if (targetOrigin.position.y < mypos.y && Mathf.Abs(mypos.y - targetPos.y) > Mathf.Abs(mypos.x - targetPos.x))
{
    print("上");
}
else if (Mathf.Abs(mypos.y - targetPos.y) > Mathf.Abs(mypos.x - targetPos.x))
{
    print("下");
}
英文:

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

if (targetOrigin.position.x &lt; mypos.x &amp;&amp; Mathf.Abs(mypos.x - targetPos.x) &gt; Mathf.Abs(mypos.y - targetPos.y))
{
    print (&quot;Right&quot;);
}
else if (Mathf.Abs(mypos.x - targetPos.x) &gt; Mathf.Abs(mypos.y - targetPos.y))
{
    print (&quot;Left&quot;);
}

if (targetOrigin.position.y &lt; mypos.y &amp;&amp; Mathf.Abs(mypos.y - targetPos.y) &gt; Mathf.Abs(mypos.x - targetPos.x))
{
    print (&quot;Up&quot;);
}
else if (Mathf.Abs(mypos.y - targetPos.y) &gt; Mathf.Abs(mypos.x - targetPos.x))
{
    print (&quot;Down&quot;);
}

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:

确定