英文:
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 < mypos.x && changed(mypos.y, targetPos.y))
{
print ("Right");
}
else if (changed(mypos.y, targetPos.y))
{
print ("Left");
}
if (targetOrigin.position.y < mypos.y && changed(mypos.x, targetPos.x))
{
print ("Up");
}
else if (changed(mypos.x, targetPos.x))
{
print ("Down");
}
}
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 < mypos.x && Mathf.Abs(mypos.x - targetPos.x) > Mathf.Abs(mypos.y - targetPos.y))
{
print ("Right");
}
else if (Mathf.Abs(mypos.x - targetPos.x) > Mathf.Abs(mypos.y - targetPos.y))
{
print ("Left");
}
if (targetOrigin.position.y < mypos.y && Mathf.Abs(mypos.y - targetPos.y) > Mathf.Abs(mypos.x - targetPos.x))
{
print ("Up");
}
else if (Mathf.Abs(mypos.y - targetPos.y) > Mathf.Abs(mypos.x - targetPos.x))
{
print ("Down");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论