TransWikia.com

Player is not looking at correct direction

Game Development Asked on November 20, 2021

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. 🙁

2 Answers

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.

Answered by Swati on November 20, 2021

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

Answered by Adrien G. on November 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP