Reputation: 203
My Enemy/Bot is facing the wrong direction as shown in the attached screenshot 1. Basically what I want is that Enemy/Bot gun should directly face the player, not enemy/Bot himself.
Note: The white line in the image is representing Aim direction which is correct.
Here is my Code:
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);
What I Get from the above code is this: (Screenshot # 1)
What I want is this: (Screenshot # 2)
Gun position And Player position at Aim Animation State.
Any help is appreciated. I know its simple problem but I struggling now. :(
Upvotes: 1
Views: 76
Reputation: 90739
As you can see in your added Screenshots the gun's direction does not match with the bot's forward axis (blue vector).
You could add this difference to your target rotation like e.g.
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);
One problem left though is that still the Gun model doesn't even match the gunDirection
's forward vector so there will still be an offset left!
Upvotes: 1