x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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

Product Version: UE 4.17
Tags:
more ▼

asked Dec 20 '17 at 01:50 PM in Blueprint Scripting

avatar image

Samuelb
273 9 17 22

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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;
 }
 
more ▼

answered Dec 20 '17 at 02:02 PM

avatar image

Jin_VE
4.4k 32 77 41

avatar image Samuelb Dec 20 '17 at 02:14 PM

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++?

avatar image Jin_VE Dec 20 '17 at 02:20 PM

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.

avatar image Samuelb Dec 20 '17 at 02:44 PM

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

avatar image Samuelb Dec 21 '17 at 11:44 AM

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?

avatar image Jin_VE Dec 21 '17 at 12:27 PM

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?

avatar image Samuelb Dec 21 '17 at 12:59 PM

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.

avatar image Jin_VE Dec 21 '17 at 01:14 PM

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".

avatar image Samuelb Dec 21 '17 at 01:27 PM

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: alt text

avatar image Samuelb Dec 21 '17 at 01:29 PM

EventGraphexecution

btsbraingraph.png (230.8 kB)
avatar image Jin_VE Dec 21 '17 at 01:34 PM

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.

avatar image Samuelb Dec 21 '17 at 01:38 PM

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

avatar image Samuelb Dec 21 '17 at 01:44 PM

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

avatar image Jin_VE Dec 21 '17 at 01:52 PM

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.

avatar image Jin_VE Dec 21 '17 at 01:56 PM

My max acceleration is 2048. Oh and there's another option in the movement component called "Requested Move Use Acceleration". Enable that.

avatar image Samuelb Dec 21 '17 at 02:08 PM

Ok i'll try it. Mean while i made a quick paint example that might clear up what i mean. alt text

avatar image Samuelb Dec 21 '17 at 02:18 PM

the breaking deceleration was the cause of the slow movement.

avatar image Samuelb Dec 21 '17 at 02:38 PM

OK, I think i got it working in the Bot Character Bp with your math, i'm going to test if it works for the animation activation.

avatar image Samuelb Dec 21 '17 at 03:04 PM

Holy crap its working! Jin_Ve you are a fracking unreal 4 GOD! Massive props to you dude, thanks for the help!

avatar image Jin_VE Dec 21 '17 at 03:11 PM

Hehe. Glad to hear. See you around.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

https://docs.unrealengine.com/latest/INT/BlueprintAPI/Math/Rotator/FindLookatRotation/

https://docs.unrealengine.com/latest/INT/BlueprintAPI/Math/Rotator/InvertRotator/index.html

more ▼

answered Dec 21 '17 at 11:54 AM

avatar image

Vaheva
1.3k 24 25 49

avatar image Samuelb Dec 21 '17 at 12:07 PM

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.

avatar image Vaheva Dec 21 '17 at 12:11 PM

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.

avatar image Vaheva Dec 21 '17 at 12:21 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question