玩家没有看正确的方向

Player is not looking at correct direction

我的Enemy/Bot朝向错误的方向,如附图1所示。基本上我想要的是Enemy/Bot枪应该直接面对玩家,而不是enemy/Bot自己。

Note: The white line in the image is representing Aim direction which is correct.

这是我的代码:

Vector3 targetPosition = kbcrd.playerToEngage.thirdPersonPlayerModel.shootTarget.position;
targetPosition .y = 0;

Vector3 botPosition = pb.thirdPersonPlayerModel.gunDirection.position;
botPosition.y = 0;

Quaternion targetRotation = Quaternion.LookRotation(targetPosition - botPosition);

//pb is Bot which is holding transform and other scripts.
pb.transform.rotation = Quaternion.RotateTowards(pb.transform.rotation, targetRotation, kbcrd.lookingInputSmooth);

我从上面的代码中得到的是这样的:(截图#1)

我想要的是:(截图#2)

枪支和玩家方向:

瞄准动画状态下的枪支位置和玩家位置。

感谢任何帮助。我知道它的简单问题,但我现在正在努力。 :(

正如您在添加的屏幕截图中看到的那样,枪的方向与机器人的前轴(蓝色矢量)不匹配。

您可以将此差异添加到您的目标旋转中,例如

var targetPosition = kbcrd.playerToEngage.thirdPersonPlayerModel.shootTarget.position;
targetPosition.y = 0f;

var botPosition = pb.thirdPersonPlayerModel.gunDirection.position;
botPosition.y = 0f;

var gunForward = gunDirection.forward;
gunForward.y = 0f;

var botForward = pb.transform.forward;
botForward.y = 0f;

// Get the offset between the bot's and the gun's forward vectors
var gunBotOffset = Vector3.SignedAngle(gunForward, botForward, Vector3.up);
// Get a rotation to rotate back from the gun to the player direction
var gunToBotRotation = Quaternion.AngleAxis(gunBotOffset, Vector3.up);

// Add the back rotation to the target rotation
var targetRotation = Quaternion.LookRotation(targetPosition - botPosition) * gunToBotRotation;

pb.transform.rotation = Quaternion.RotateTowards(pb.transform.rotation, targetRotation, kbcrd.lookingInputSmooth);

还有一个问题是,枪支模型仍然不匹配 gunDirection 的前向矢量,所以仍然会有一个偏移量!