# how do i calculate the X and Z of an object position when it is moving?

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

## Recommended Posts

Hello, i have an object who is an enemy, currently it constantly looks at the player by rotating the Y angle of the object with the code below. However now i want it to always move towards the player's x,y,z position, however i am only able to change the x,y,z of the enemies position. Is there any calculations i can use to change these values so the enemy moves closer to the player a a certain amount each frame, such as 2 units closer each iteration. Any help would be appreciated, The look at code in use: XDiff = m_ReceptionObject.GetXPos() - m_CharacterObject.GetXPos(); ZDiff = m_ReceptionObject.GetZPos() - m_CharacterObject.GetZPos(); Direction3 = (float)atan2(XDiff, ZDiff); m_ReceptionObject.Rotate(0.0f,Direction3,0.0f);

##### Share on other sites
Quote:
 Original post by bigbrother2000Hello, i have an object who is an enemy, currently it constantly looks at the player by rotating the Y angle of the object with the code below. However now i want it to always move towards the player's x,y,z position, however i am only able to change the x,y,z of the enemies position. Is there any calculations i can use to change these values so the enemy moves closer to the player a a certain amount each frame, such as 2 units closer each iteration. Any help would be appreciated,The look at code in use:XDiff = m_ReceptionObject.GetXPos() - m_CharacterObject.GetXPos(); ZDiff = m_ReceptionObject.GetZPos() - m_CharacterObject.GetZPos(); Direction3 = (float)atan2(XDiff, ZDiff); m_ReceptionObject.Rotate(0.0f,Direction3,0.0f);

You mean like using a unit vector based on that direction angle (facing of enemy)

x = x + sin(Direction3) * enemyspeed;
z = z + cos(Direction3) * enemyspeed;

??????

(you may want to prorate the enemyspeed by the framerate....)

##### Share on other sites
yeh i want to move the object in the direction its facing, so update the x,z of the enemies position each frame so it gets closer to the player.

1. 1
2. 2
3. 3
Rutin
18
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633659
• Total Posts
3013216
×