Rotate a standing still enemy that's using a character movement component

Hello, I have an enemy that’s able to move around and move towards a player, but I have some issues with defining the whole moving behavior.

In my behavior tree I have a Move to target BTTask, that’s followed by an Attack target BTTask. They do their job as supposed, but the issue arises whenever a target moves around the enemy very closely, i.e. without requiring the enemy to move towards the target once again. In this case the enemy will be close enough to the target, but facing a wrong direction – the one that isn’t towards the target, and after that Attack target will be executed leading the enemy to attack nothing.

My enemy uses UCharacterMovementComponent, and so I’m looking for something that can be used with it. I think that the functionality that will satisfy this task can be placed also as a BTTask between the Move and Attack ones.

I also would like to know how many degrees the enemy has rotated in a frame in order to be able animate it as supposed.

So my question is: How can I rotate an AI still character that uses UCharacterMovementComponent using its RotationRate towards a player, and also know how many degrees it rotated by in a frame?

Thanks for reading.

Hi,
You can attach service to your task to add more functionality, if you don’t want to have rotating behavior in the Attack task. Rotation speed depends on the attack, but I would keep it light, so you don’t see the enemy snapping or doing crazy moves if the player jerks/dodges really hard.

My game isn’t really player battle based (i.e. the player doesn’t really have to fight that much), so the available movement for the players isn’t that much. It’s ok if the AI character will have to stand still for even a second or two trying to rotate in order to face the player that’s trying to just run around it.

At the moment my AI character will stand nearby a player, and attack the direction the AI character got from moving towards the player, and if the player just goes to around without going too far from acceptance radius (the one I pass to MoveTo node), then the AI character will still attack the way it was attacking before.

In that case I would put the rotation as part of the attack task and rotate the character slowly towards the direction of the enemy.

Well, my actual question is how to rotate a character using CMC :smile:

I think what you want is to take forward vector of the attacking character and with unit direction(I think) get direction towards the enemy. Then you can use dot product to calculate the direction in which your character should rotate. After the character rotates close enough to the enemy, you can stop the rotation. Rotation you can add on tick and use constant value, or modify it base on the attack animation or how much the character need to rotate, depends on what you want to do.

Probably I should’ve specify that maths isn’t the problem, but the functions to use.
If I will simply use SetActorRotation, the character will not be rotated because it’s using CMC that’s managing its movement wise transforms (i.e. rotation and location), so I’m looking for something that allows you to rotate the character that uses CMC without actually moving it instead.

I have found a solution for that problem. To make it work there are some parameters to tweak and a service to set.

First of all the AI character must not use controller rotation, thus go on your AI pawn and uncheck Use Controller Rotation Pitch/Yaw/Roll.
image

After that go on Character Movement Component and uncheck Orient Rotation to Movement, and check Use Controller Desired Rotation instead.
image

Since my blackboard has the Target I want to rotate my AI pawn towards, it’s possible to use Default Focus service. The service takes an Actor as input, and assigns it to the Focus Actor on AIController. Note: my Target field is null whenever the AI shouldn’t have any.
I have set it to the main selector I have in my Behavior Tree, but it may be a different place for someone else.

image

The last thing is to go on your custom AIController, and check Set Control Rotation from Pawn Orientation. Regardless the field name, the controller will be rotated towards the Focus Actor if there is any, otherwise it’ll rotate to the pawn orientation.

Hopefully my answer will help someone else who’s struggling with the same problem. :smile:

1 Like