0
\$\begingroup\$

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) enter image description here


What I want is this: (Screenshot # 2) enter image description here

Guns And player Orientation: enter image description here

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.enemyPlayersAwareOff.Add(botCampaignModeAI.detectedEnemies);
        }

        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. :(

\$\endgroup\$
1
\$\begingroup\$

Check this link. In your case, it would be, I think:

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

\$\endgroup\$
9
  • \$\begingroup\$ 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". \$\endgroup\$
    – Swati
    Jun 22 '20 at 7:19
  • \$\begingroup\$ Can you send full script? \$\endgroup\$
    – Adrien G.
    Jun 22 '20 at 7:29
  • \$\begingroup\$ 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? \$\endgroup\$
    – Swati
    Jun 22 '20 at 7:31
  • \$\begingroup\$ Just send the Update Function. Your code is from here right? \$\endgroup\$
    – Adrien G.
    Jun 22 '20 at 7:32
  • 1
    \$\begingroup\$ 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. :) \$\endgroup\$
    – Swati
    Jun 22 '20 at 9:20
0
\$\begingroup\$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.