简体   繁体   English

如何从玩家那里检测游戏对象是左侧还是右侧

[英]How to detect if the game object is left or right from the player

The Debug.Log always returns LEFT while it needs to be only right when it's on the right side of the player. Debug.Log总是返回LEFT,而它只有在播放器右侧时才需要正确。 Now it's showing both left and right (when on the right side of the game object). 现在它同时显示左右(当在游戏对象的右侧时)。

if (distance <= 249)
        {
            if (enemy.transform.position.x > player.transform.position.x)
            {
                if (waitTime == 0)
                {
                    Debug.Log("LEFT");
                    FireGunsLeft();
                }
            }
            else 
            {
                if (waitTime == 0)
                {
                    Debug.Log("RIGHT");
                    FireGunsRight();
                }
            }

        }

Use player.transform.InverseTransformPoint(enemy.transform.position) 使用player.transform.InverseTransformPoint(enemy.transform.position)

You can use Transform.InverseTransformPoint to find the enemy's relative position from the perspective of the player. 您可以使用Transform.InverseTransformPoint从玩家的角度查找敌人的相对位置。

Vector3 enemyDirectionLocal = player.transform.InverseTransformPoint(enemy.transform.position);

This Vector3 enemyDirectionLocal is a vector that describes the enemy's position offset from the player's position along the player's left/right, up/down, and forward/back axes. 这个Vector3 enemyDirectionLocal向量是一个向量,它描述了敌人沿玩家的左/右,上/下和前进/后退轴偏离玩家位置的位置。

What this means is that if enemyDirectionLocal.x is less than zero, it's on the left side of the player (although maybe ahead or behind as well), and if it's greater than zero, it's on the right side. 这就是说,如果enemyDirectionLocal.x小于零,则它位于播放器的左侧(尽管也可能位于前方或后方),如果大于零,则位于播放器的右侧。 If it's zero, it's directly behind or ahead of the player. 如果为零,则直接位于播放器的后面或前面。

Vector3 enemyDirectionLocal = player.transform.InverseTransformPoint(enemy.transform.position);

if (enemyDirectionLocal.x < 0)
{
    if (waitTime == 0)
    {
        Debug.Log("LEFT");
        FireGunsLeft();
    }
}
else if (enemyDirectionLocal.x > 0)
{
    if (waitTime == 0)
    {
        Debug.Log("RIGHT");
        FireGunsRight();
    }
}

Another approach would be to find the Vector between the player's position and the enemy's position var directionToEnemy = enemy.transform.position - player.transform.position and then find its projection on the right vector of the player's transform, having it > 0 when the enemy is on the right of the player: 另一种方法是找到玩家位置与敌人位置之间的Vector var directionToEnemy = enemy.transform.position - player.transform.position敌人var directionToEnemy = enemy.transform.position - player.transform.position ,然后在玩家变换的right矢量上找到其投影,当敌人在玩家的右边:

var directionToEnemy = enemy.transform.position - player.transform.position;
var projectionOnRight = Vector3.Dot(directionToEnemy, player.transform.right);

if (projectionOnRight < 0)
{
    if (waitTime == 0)
    {
        Debug.Log("LEFT");
        FireGunsLeft();
    }
}
else if (projectionOnRight > 0)
{
    if (waitTime == 0)
    {
        Debug.Log("RIGHT");
        FireGunsRight();
    }
}

You also might consider doing the waitTime == 0 check before you do the calculations for the relative position. 您还可以考虑在计算相对位置之前先执行waitTime == 0检查。

if Mathf.Approximately(projectionOnRight, 0f) returns true, then the enemy is right in front of the player and you might want to perform FireGunsStraight() or whatever you have for this case. 如果Mathf.Approximately(projectionOnRight, 0f)返回true,那么敌人就在玩家的正前方,您可能想要执行FireGunsStraight()或在这种情况下执行的任何操作。

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

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