本文主要是介绍预判修改方向,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/// <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;
}
备注:需要提前检测,例如触发检测
这篇关于预判修改方向的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!