# Target Following

This topic is 4238 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to have an enemy looking at the player and track him as he flys around in 3D space. I have the position of the player, the position of the enemy, and the current direction of the enemy. I'd like to adjust the yaw, pitch and roll of the enemy to aim towards the player. My problem is that I don't know what vector calculations to perform to determine how much yaw, pitch, and roll to add to get the enenmy aiming towards the player. I currently have just the yaw tracking, but it doesn't completely work. It only works for 180 degrees, the other 180 degrees the enemy faces away from the player.
			// get target's position
Vector3 targetPos = Target.GetPosition();

// calc axis to target
Vector3 hereToTarget = targetPos - Position;
hereToTarget.Normalize();

// calc difference between the line to the target and the view axis
Vector3 facingDifference = hereToTarget - ViewAxis;

// adjust Yaw for X differnce
if ( facingDifference.X > 0.1f )
{
LogicActions[ (int)Actions.Yaw ] = 1.0f;
}
else if ( facingDifference.X < -0.1f )
{
LogicActions[ (int)Actions.Yaw ] = -1.0f;
}



##### Share on other sites
I'm sure I could come up with something using traditional vector math (sin and cos and all that fun stuff [grin]) but I haven't a clue how one uses the Vector functions and stuff. Sorry.

• 23
• 10
• 19
• 15
• 14