Rotate enemy dynamically between target or path?

Hey guys

I have set up a movement logic using A* I’m happy with. Using AIDestinationSetter along with FollowerEntitity. Works great. But I’m having a lot of trouble figuring out a way to control the enemy’s rotation in a natural way.

The behavior I’m looking for is to control where the enemy is looking at different times. Running at full speed down a hallway around corners? Face the movement direction as is the default, great. Moving towards a location to search for something? Face the movement direction as well.

But when chasing a player and the enemy is close to the player, perhaps when moving around a table? The enemy should look at the player instead. It should perform a strafing movement. As one would when chasing (or even how a player moves in an FPS game).

I read that FollowerEntity.SetDesination() has a facingDirection overload, but I can’t seem to make it work as intended. It more behaves like a way to “hone in on the position” when approaching the destination. But that’s not what I want. The way the enemy movies position wise is great - it’s only the “visual look” of the rotation that is to be modified.
But anyways, I would love to not even use FollowerEntity.SetDesination()… as I am using AIDestinationSetter.target() to continously track a moving object (the player) as I can understand that is the prefered way to handle tracking moving targets.

Using these two components - how do I control the rotation of the enemy in a dynamic way? To switch back and forth of the default behavior… but take control of it, even lerping it, when needed?

Thanks

/Alex

  • A* version: 5.2.4
  • Unity version: 6.000.023f1

Have you looked into the rotationSync property? Seems to be what you’re looking for :smiley:

I tried it briefly, and setting it to false did indeed seemingly freeze the rotation.

My thoughts was then to try to change the transform.rotation of the agent in any Update() loop, but that did not affect it. My thought was that A* is overwritting it somewhere internally, so I thought that wasn’t the right way to go.

But I guess I was wrong? So the idea is to set rotationSync to false and then update the rotation through code where exactly?

Hmm, I got it to work in Update()

void Start(){
    GetComponent<FollowerEntity>().updateRotation = false;
}

void SetRandomRotation(){
    float3 rot = new float3(0f, Random.Range(0f, 365f), 0f);
    transform.rotation = Quaternion.Euler(rot);
}

void Update(){
    SetRandomRotation();
}

(I also had a [Button] attribute from Odin Inspector on the actual random function to test it, which is why I seperated it out)

This code had my agent setting a random rotation every frame with no issues- this is what you tried I’m assuming?