/// <summary>
/// 是否闪避修改方向
/// </summary>
/// <param name="ArmsObject">武器对象</param>
/// <param name="ArmsVelocity">武器线速度</param>
/// <param name="CurrentObject">当前对象</param>
/// <param name="CurrentObjectMoveDir">当前对象移动方向</param>
/// <param name="CurrentObjectMoveSpeed">当前对象移动速度</param>
/// <param name="PrejudgeDistance">预判距离</param>
/// <returns></returns>
public bool IsDodgeChangeDir(GameObject ArmsObject, Vector3 ArmsVelocity, GameObject CurrentObject, Vector3 CurrentObjectMoveDir, float CurrentObjectMoveSpeed, float PrejudgeDistance)
{
//(武器对象点)到(当前对象移动方向向量)之间的垂直距离
float fProj = Vector3.Dot(ArmsObject.transform.position - CurrentObject.transform.position, CurrentObjectMoveDir.normalized);
float PointToLineDistance = Mathf.Sqrt((ArmsObject.transform.position - CurrentObject.transform.position).sqrMagnitude - fProj * fProj);
//(武器移动方向的反方向)与(当前对象移动方向的方向)之间的夹角
float dot = Vector3.Dot(-ArmsVelocity.normalized, -CurrentObjectMoveDir);
//计算武器移动的距离
float ArmsObjectMoveLength = Mathf.Sqrt(Mathf.Pow(PointToLineDistance, 2) / (1 - Mathf.Pow(dot, 2)));
//
Vector3 ArmsVelocityNormalized = ArmsVelocity.normalized;
float ArmsObjectSpeed = 0;
if (ArmsVelocity.x != 0)
{
ArmsObjectSpeed = ArmsVelocity.x / ArmsVelocityNormalized.x;
}
else if (ArmsVelocity.z != 0)
{
ArmsObjectSpeed = ArmsVelocity.z / ArmsVelocityNormalized.z;
}
//武器以(武器移动的速度)的速度在(武器移动的距离)的距离上移动所需时间
float t = ArmsObjectMoveLength / ArmsObjectSpeed;
//当前对象在规定时间移动后的坐标
Vector3 CurrentObjectEndPos = CurrentObject.transform.position + (CurrentObjectMoveDir * CurrentObjectMoveSpeed * t);
//武器对象在规定时间移动后的坐标
Vector3 ArmsObjectEndPos = ArmsObject.transform.position + ArmsVelocity * t;
//(武器对象在规定时间移动后的坐标)与(当前对象在规定时间移动后的坐标)之间的距离
float distance = Vector3.Distance(ArmsObjectEndPos, CurrentObjectEndPos);
//预判距离
if (distance < PrejudgeDistance)
{
Debug.Log("预判距离" + distance);
return true;
}
return false;
}
备注:需要提前检测,例如触发检测