Rotation of an enemy based on a player's position[Problem]

Hi my English is very bad so ignore spelling mistakes, I’m making an enemy that follows the player when the player moves to a certain area of my enemy’s local position, so far it’s ok the enemy moves towards the player, the problem is that the enemy doesn’t turn to line up with the player and I don’t know what calculation I should make for my enemy to turn and line up with the player, my enemy has access to that player’s position when the player enters the given area.

Everything is being done through UPBGE 2.5 components.

I already appreciate any advice on how I can make this work.

look up alignAxisToVect()

def face_direction(obj, vector):
    track_axis = 1 # x,y,z = 0,1,2
    turn_speed = 1.0 # 0.0 - 1.0
    # face direction
    obj.alignAxisToVect(vector, track_axis, turn_speed)
    # head up
    obj.alignAxisToVect([0.0,0.0,1.0], 2, 1.0)

I have to transform the player’s global position into a vector?

Edit 01: Rephrasing what I said, this vector that I have to pass is what exactly? (Not sure how to use this function)

pas the obj you want to align and a worldPosition as vector. Object.worldPosition is already a vector so you can use it directly.

Thank you very much it worked, it wasn’t working before because there was a condition that was conflicting with alignAxisToVect(), but now it’s resolved.

I noticed that this code has a small problem, when the move is applied to the object it doesn’t finish spinning to completely align with the player, so if the player just stops moving, the enemy will stop spinning and won’t walk directly into towards him. , this problem is in the enemy’s rotation speed, which even in 1.0 is slow because the speed he spins adapts to the speed the player walks, is there any way to make him spin faster regardless of the player’s speed?

(I tried using a raycast() to see if I would change something, but I didn’t have any difference)

redo the moving, script works fine and doesn’t create your problem. 1.0 speed is instant so no you can not make it faster.

I guess you don’t deactivate stuff when enemy reaches the player/spot. Or are you turning the object as well? Don’t do that he only needs movement on y axis(forwards)

OK, got it, I’m going to redo the script and check that there are no errors in the script conditions

the basics are simple.

With states you can simply do:

state 1

  • player nearby
  • go to state 2

state 2

  • follow and rotate to player
  • if close to player state 3

state 3

  • attack
  • if dead or out of range go to state 4

state 4

  • return to start location
  • go to state 1

make the enemy follow the interaction steps so he doesn’t do it all at once unnecessarily?