Line Casting: GameObject Face 射线的端点
Line Casting: GameObject Face End Point of Ray
我正在尝试让敌方对象面向线投射的末端。当正确检测到时,我的代码会将射线锁定到播放器上。我的问题是我的敌人精灵是 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, end.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, 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, 0, enemyRot);
}
当玩家在屏幕上移动时,敌人根本不面向玩家并且似乎根本没有旋转太多。
我试过应用 rotationSpeed 变量并将其乘以 enemyRot 乘以 Time.deltaTime 但也没有用。
enemyRb.transform.GetChild(0).GetChild(0).eulerAngles = new Vector3(0, 0, enemyRot * rotSpd * Time.deltaTime);
我已经坚持了一个星期了,真的需要一些帮助来解决这个问题!那么如何使用 eulerAngles 实现我想要的旋转?
原来我做的数学题太多了。这是解决方案:
编辑:我的敌人面朝上。此代码适用于该规模。
void PlayersPosition()
{
facePlayer = player.position - enemyRb.transform.GetChild(0).GetChild(0).position;
enemyRb.transform.GetChild(0).GetChild(0).up = -facePlayer;
}
我正在尝试让敌方对象面向线投射的末端。当正确检测到时,我的代码会将射线锁定到播放器上。我的问题是我的敌人精灵是 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, end.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, 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, 0, enemyRot);
}
当玩家在屏幕上移动时,敌人根本不面向玩家并且似乎根本没有旋转太多。
我试过应用 rotationSpeed 变量并将其乘以 enemyRot 乘以 Time.deltaTime 但也没有用。
enemyRb.transform.GetChild(0).GetChild(0).eulerAngles = new Vector3(0, 0, enemyRot * rotSpd * Time.deltaTime);
我已经坚持了一个星期了,真的需要一些帮助来解决这个问题!那么如何使用 eulerAngles 实现我想要的旋转?
原来我做的数学题太多了。这是解决方案:
编辑:我的敌人面朝上。此代码适用于该规模。
void PlayersPosition()
{
facePlayer = player.position - enemyRb.transform.GetChild(0).GetChild(0).position;
enemyRb.transform.GetChild(0).GetChild(0).up = -facePlayer;
}