线条投射:GameObject面的光线终点

我正在尝试使敌人的对象面对强制转换的结尾。当正确检测到时,我拥有的代码将射线锁定在播放器上。我的问题是我的敌人精灵是Rigidbody2D的孙子,我正在使用eulerAngles更改精灵方向。这是我的代码:

using UnityEngine;

public class PlayerDetection : MonoBehaviour
{
public Transform origin,end,player;
public float radarSpd;
public bool playerDetected;

public static bool playerIsDetected;

private int playerLayer = 1 << 8;
private Rigidbody2D enemyRb;
private Vector3 facePlayer;

private void Start()
{
    enemyRb = getcomponentInParent<Rigidbody2D>();
    playerIsDetected = false;
}

private void Update()
{
    PlayerDetector();
    if (playerDetected == false)
    {
        Radar();
    }
    else { PlayerIsDetected(); }

}

void PlayerDetector()
{
    Debug.DrawLine(origin.position,end.position,Color.red);
    playerDetected = Physics2D.Linecast(origin.position,playerLayer);
}

void Radar()
{
    end.RotateAround(origin.position,Vector3.forward,radarSpd * Time.deltaTime);
}

void PlayersPosition()
{
    facePlayer = player.position - enemyRb.transform.getchild(0).getchild(0).position;
    float enemyRot = Mathf.Atan2(facePlayer.y,facePlayer.x) * Mathf.Rad2Deg;
    enemyRb.transform.getchild(0).getchild(0).eulerAngles = new Vector3(0,enemyRot);

}

void PlayerIsDetected()
{
    if(playerDetected == true)
    {
        playerIsDetected = true;
        end.position = player.position;
        PlayersPosition();
    }
}
}

我需要帮助的这段代码的重点是:

void PlayersPosition()
{
facePlayer = player.position - enemyRb.transform.getchild(0).getchild(0).position;
float enemyRot = Mathf.Atan2(facePlayer.y,facePlayer.x) * Mathf.Rad2Deg;
enemyRb.transform.getchild(0).getchild(0).eulerAngles = new Vector3(0,enemyRot);

}

当玩家在屏幕上移动时,敌人根本不会面对玩家,似乎也不会旋转太多。

我尝试应用了rotationSpeed变量,然后将其乘以敌人Rot再乘以Time.deltaTime,但也无济于事。

 enemyRb.transform.getchild(0).getchild(0).eulerAngles = new Vector3(0,enemyRot * rotSpd * Time.deltaTime);

我已经坚持了一个星期,确实需要一些帮助来解决此问题!那么如何通过使用eulerAngles实现所需的轮换?

magichuang123 回答:线条投射:GameObject面的光线终点

原来我做的数学太多。解决方法如下:

编辑:我的敌人面朝上。这段代码可以达到这种规模。

void PlayersPosition()
{
    facePlayer = player.position - enemyRb.transform.GetChild(0).GetChild(0).position;
    enemyRb.transform.GetChild(0).GetChild(0).up = -facePlayer;
}
本文链接:https://www.f2er.com/3131119.html

大家都在问