Ai Check which direction bot is moving

Hey there

I have a Ai bot that’s based on a playable character in my 1vs1 fighting game. My characters directional animations are based on the input axis it receives from the players controls. How can i calculate which direction the bot is moving relative to the enemyplayer, is there a way to receive input axis from the aicontroller? The bot and player are constantly looking at eachother.

Any Advice would be greatly appreciated!

-SamuelB

Hey Jin_VE thanks for the quick response!
Yeah looks pretty similar to what i was trying to do,however i’m doing it in blueprint. I’m leaving my office for today, but i’ll give it a shot tomorrow. It looks like i could implement this in blueprint directly based on that code, or will this require me to use c++?

There are no input axes from the controller AFAIK (and I looked in the code). I had to take GetCurrentAcceleration() and GetMaxAcceleration() and work backwards to generate input axes.

Here’s the function I call to get the input axes. If it’s not enough for you to run with, let me know.

void ABotController::GetMovementInput(float& MoveForward, float& MoveRight)
{
	APawn* pawn = GetPawn();
	if(pawn != NULL)
	{
		UCharacterMovementComponent* mc = Cast<UCharacterMovementComponent>(pawn->GetMovementComponent());
		if(mc != NULL)
		{
			FVector accv = mc->GetCurrentAcceleration();
			float acc = accv.Size();
			accv.Normalize();
			float max = mc->GetMaxAcceleration();
			float x = acc / max;
			FVector inp = accv * x;

			FVector forward = pawn->GetActorForwardVector();
			FVector right = pawn->GetActorRightVector();

			MoveForward = FVector::DotProduct(inp, forward);
			MoveRight = FVector::DotProduct(inp, right);
			return;
		}
	}

	MoveForward = 0.0;
	MoveRight = 0.0;
}

Yeah you should be able to translate it into blueprint nodes. As long as you can get the current and max acceleration values, which I believe you can. Let me know if you hit a roadblock.

Yeah thats what i figured aswell, i’ll keep you posted on my progress. Have a nice day!

Ok i managed to get everything calculated from your code. However, i’m getting 0 from the current acceleration even though the bot is moving. Apparently the character movement component does not take into account the “ai move to” function as acceleration. I must be doing something wrong?

Hey thanks for the answer.
I did try using the findlookatlocation, however i was unable to get the values I needed. I’ll give it another try soon, if I can’t get Jin_VE’s solution to work.

With blueprint you can use FindLookAtLocation, where location is your second player (or other Actor) location. If you want to use opposite direction, you can add InvertRotator (negate like) to this direction. This all is easy to do with blueprints and here ready functions.

I use this a lot - for sure it work. I will have open editor later today, I can look for samples. You just need two actor locations connected with Find Look At Rotation.

This find look at location give will add you all need data:

  • player / ai rotation
  • player direction you have already
  • now just cast to ai to get ai rotation

with this data you can easy calculate direction the bot is moving. I think you should just get ai direction not from controller, but this is up to you.

The acceleration is in world space. That’s why we do all the math. We’re projecting the world acceleration vector onto our forward and right vectors. At that point it will be in local space, relative to the bot. My math doesn’t do any more than that. TBC: if the bot was moving forward relative to itself then the forward axis value would be positive and the right axis value would be zero. Moving to the right, forward would be zero and right would be positive.

If the bot axis values are changing based on your player position then you must be doing more math – or you didn’t translate my math correctly perhaps.

My response is missing… I responded to your most recent comment (also missing).

Before I retype it all, I want to be clear. Your AI is moving around and you want the legs to look like they are walking in the right direction, correct? In other words, you want to give the animation blueprint the same kind of forward/right values that the player gives it. The bot may need to move relative to the player but that is a separate issue. Or have I misunderstood?

Yeah sorry I didn’t see you response and i ran into another issue, so in other words I haven’t managed to get any values out and afterwards i deleted my comment.

You seem to understand me correctly, the thing is the bots rotation is being changed so it constantly looks at the enemy player as it moves to the next location based on where the enemy player is. So the forward direction is basically constantly changing based on the bots rotation if that makes any sense.

Right but no matter what the bot is doing, you want the legs to move so it looks like he’s walking right. Like strafing. OK so in the CharacterMovementComponent of your bot, enable “Use Acceleration for Paths”.

Ok That made the bot move very slowly and i’m still not getting any values from current accel.
I’m implementing the function in my BTService (bot brain), could that be an issue?
screen cap of how i’m setting the variables:

Jeez. I don’t know if that will work. Since movement is so realtime, I did it in my bot’s tick event.

Get it into the Tick event and put a Print String in there to see if the acceleration value isn’t zero.

Ok, I’ll try doing it inside my botCharacters event tick and see what happens, thanks for the help and patience so far! =)

The value is zero if the " Use acceleration for Paths" is disabled, but the bot moves very slowly if its enabled. The bot also gets slower if i increase the max walkspeed. Wow i’m getting super confused right now ;D

Dunno. Could be the friction with the ground or the braking. Um. My walk speed is 400, braking friction is 0, and braking deceleration is 900.

And what do you do with the axis values? You should only be forwarding them to the anim BP. Don’t use them to move the bot or anything like that.