我有一个寻的导弹,但我想通过射线投射增加一些指导,以避开墙壁或建筑物。我不想急转弯,而是要平稳地检查是否有墙要绕过,并顺利绕过它,以继续瞄准敌人。
我该怎么做。我知道使用raycast可以检查并设置一定的距离,但是我不知道如何平稳地移动到另一个位置来避免障碍物。
我的代码当前直接到达目标击中对象的位置。
// Add force
ownMissileBody.velocity = transform.forward * missileSpeed;
// Allow missile to turn
if (allowTurning)
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 1000f))
{
if (hit.transform.tag == "Building")
{
Debug.Log("Missile sees Building in front!");
}
else
{
// Set homing turning/rotation
var missileTargetRotation = Quaternion.LookRotation(enemyTarget.localPosition - transform.localPosition);
// Turn Missile to Target
ownMissileBody.MoveRotation(Quaternion.RotateTowards(transform.localRotation, missileTargetRotation, missileTurnSpeed));
}
}
}
对我来说,转向行为可以为您提供一个很好的解决方案。
特别是我将Avoidance behavior和Pursuit behaviour合并。
通过将力结果组合在一起,您可以使整洁的导弹正常工作。
不过,我建议您注意的一点是,不要使用光线投射,因为它会发出一条直线;而您的对撞机可能比它大得多;因此,根据您的对撞机,我会考虑使用Physics.SphereCast或Physics.BoxCast代替