# Player is not looking at correct direction

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: 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)

Guns And player Orientation:

Full function if needed:

public override void WriteToPlayerInput(Kit_PlayerBehaviour pb)
{
CampaignModeControlRuntimeData kbcrd = pb.botControlsRuntimeData as CampaignModeControlRuntimeData;
CampaignModeEnemyAI botCampaignModeAI = pb.gameObject.GetComponent<CampaignModeEnemyAI>();

if (botCampaignModeAI.enemyInRange)
{
if (kbcrd.enemyPlayersAwareOff.Contains(botCampaignModeAI.detectedEnemies) == false)
{
}

kbcrd.playerToEngage = botCampaignModeAI.detectedEnemies;
//CheckForEnemies(pb, kbcrd);
}
else
{
kbcrd.playerToEngage = null;
}

UpdateAimPosition(pb, kbcrd);
FiringDecision(pb, kbcrd, botCampaignModeAI.enemyInSight);
}


Any help is appreciated. I know its simple problem but I struggling now. :(

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

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

Vector3 targetDirection = targetPosition - botPosition;

float singleStep = speed * Time.deltaTime;
Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetPosition, singleStep, 0.0f);

pb.transform.rotation = Quaternion.LookRotation(newDirection);


I hope it works, I didn't try it :)

EDIT 1: This assume that you put this code in the update function

• Thanks for your time. But unfortunately, it didn't work now bot doest now rotate but shaking on his position. I think what is happening is that bot moves toward the player a little bit and then reset direction because of that "transform.forward". Jun 22 '20 at 7:19
• Can you send full script? Jun 22 '20 at 7:29
• Sure I can but it's like more than 1000 lines of code. and a lot of other script references. Do you think it can help? Jun 22 '20 at 7:31
• Just send the Update Function. Your code is from here right? Jun 22 '20 at 7:32
• Well, I Got the issue was the offset between the gun and bot position, Bot was basically facing the correct direction but due to offset direction of the bot was off so after adding that offset it solved the issue and now it works up expectation. I will post an answer to it. And realy apretiate you time and effort. So Thanks A lot. :) Jun 22 '20 at 9:20

Thanks to @derhugo for answering this on Stack overflow Link to Answar

So the problem was basically the offset distance between the gun and the bot. So the Code mentioned in the question was just rotating Bot toward the player while not including the offset between the gun and bot. Here is the final code:

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);


Note: Mentioned code is the copy of the "@derhugo" code from stack overflow.

• Please ask each question on just one site in the StackExchange network. Cross-posts will generally be closed. Jul 22 '20 at 12:13
• @DMGregory, Sorry I didn't know that. Thanks for your advise. Jul 22 '20 at 13:04