Cannot get Velocity of AI

I created a simple AI that will follow player when it sees them. I used PawnSensing component to do this. The only problem is, I cannot find a way to check velocity to update the animation from idle to walking and vice versa.

This is the part that controls AI movement

This is the part that controls the update animation, the get velocity never return anything

1 Like

There are some settings to make AI moving and I do not know how you set the character, controller and animation blueprint. But I have some questions about your issue:

  1. Do you have a navmesh in your map?
  2. Does your character have a MovementComponent(Such as UCharacterMovementComponent)?
  3. Do you set the AIController class in your character Blueprint?
  4. How do you set the Auto Possess AI option in character Blueprint?

By the way, PawnSensing component is a old component that it would be deprecated in future. You could use AIPerception / AIPerceptionStimuliSource components for ai perception features.

1 Like

This is not the best way to do it. Of course there are a lot of ways to do what you would like however this isn’t efficient at all. First remove the PawnSensing logic or just disconnect the nodes and try doing it like this:

  1. Right click inside the AI’s folder and add new Blueprint Class - add AIController. Rename it to whatever you’d like. (MyAIController)

  2. Inside your AI character blueprint, Class Defaults, Pawn, change the AIController to the one you just created.

  3. Now in the AIController, add a new component, add AI Perception.
    image

  4. In the details panel of the AI Perception component, add a new Senses Config, and choose Sight. Configure as below:

  5. Then right click the AI Perception component and add a OnTargetPerceptionUpdated event.
    image

  6. This event can be used to detect if your AI perceives your character. Similar to the code you posted, you can make something like this:

That’s it for the simple logic of the AI. For the Anim Blueprint, you can do something like this:



image

And for the idle/walk/run state, you can create a Blendspace for the animations, like so:

Now if you have done everything correctly, when the AI sees the player, it should start moving towards him/her. Hope this helps you understand some concepts. Let me know if you need more help!

1 Like

Sorry if I didnt explain it clearly but the sprite is able to move. The problem is, it is using the default animation attached to the sprite. I was planning to use it’s velocity to detect if it moves or not and change the animation based on it but for some reason the Get Velocity function doesn’t return anything.
So regarding your question

  1. Yes I have Nevmesh set, my character able to chase my Main character until the edge of the Nevmesh
  2. Yes it has
  3. Yes I have, although I only create it and attached it on AI Controller Class
  4. I am not sure about this one

Ok, I will try to change my blueprint to use AI perception. The reason I used PawnSensing is because I saw a lot of tutorial using it

Thank you for the great guidelines. I was able to change my pawn sensing with AI perception component by following you. But the problem is still there, I cannot get any return value for my velocity function

This part here still doesn’t return any value

I do have questions regarding AI sight config, since on pawn sensing I can see how big the radius is when applied on the character, how do we check that on AI sight config?

sight radius estimation on pawnsensing
sight

Ah first of all, my bad. I didn’t check thoroughly and I missed that you are working on 2D. So all that blendspace/anim BP stuff you can check off the list! I think that best way to start off the AI on a 2D project is just copying your player character blueprint and editing out the stuff.

As to my understanding, there is no velocity getting returned because there is no movement input getting fed to the character. So to my understanding, AI Move To should not be used. Instead, you must use a logic similar to your player character. Thus, why I said it would be easier to copy off the player character.
Maybe read this post in the community, seems like what you would like to do:

For this, you can use the AI Debugger:

To enable it, go to edit–>project settings and search for debugger →

’ to enable it in game. Then you can click numpad 4 to check AI Perception. If you go ahead and go to spectator mode with TAB, you can see just that.

Don’t forget! You can utilize the AIController you created for the movement logic. Just do this stuff inside it instead of the AI Move To:

Now the velocity should be captured and the flipbooks should work as intended. I’m really not the best with 2D so I might not have the perfect solution. Let me know if this works or helped you!

1 Like

Thank you for the tips on debugging tools, I have tried it and it works well, I can see where and when exactly it sees my character.

Unfortunately I still have issue with addMovementInput function. When I tried to use addMovementInput by substracting my main character location when it was sighted and my monster location, the monster wont move at all. I already checked that some values were added to AddMovementInput function but not sure why it wont move the monster. Any idea why this happen?

1 Like

I believe you must add movement input on Event Tick. I’m really not sure how to do this in 2D however I found a solution. It’s probably not the best way to do it since it is kind of buggy but it works. Check it out:

In the AI Controller, add this Event

Then, make a boolean variable “SawPlayer” or whatever, and set/unset it on Perception Updated event, like so:

And on Event Tick (again on AI Controller), you should call Update Animation event that we just created, and then check if SawPlayer bool is true, if so, get player location + self location (AI’s location) and then compare their values on the X axis. Accordingly, we can add movement inputs. Afterwards we have to handle the rotation. We can do that by comparing the X axis again, and again accordingly, set the rotation.

Try this out and let me know it is at least close to what you’re trying to achieve. As I said, I’m not the best with 2d however with some tinkering you can improve this a lot. Good luck!

1 Like

Thank you so much for the help, I was able to get it work inspired with your idea.

I do have one other question. Since my project is a 2.5D and I am using addmovementinput, it no longer follow navmesh constraint. Do you have suggestion on how to set area limitation similar to navmesh did or use navmesh together with addmovementinput function?

The dragon is not supposedly go beyond the green navmesh

2 Likes

If you are already using the Character as your base pawn, it has a MovementComponent. You can bind a function to the MovementComponent’s OnMovementUpdated event and this will give you his current position every tick. From here you may be able to take his current position and project it to the NavMesh somehow and if it is not on the NavMesh then call the MovementComponent’s StopMovementImmediately() function. You should look at the UNavigationSystem::ProjectPointToNavigation function to see if this can help you determine if his location is or is not on the nav mesh.

Good luck!

1 Like

image

This worked for me!

Make sure to check “Use Acceleration for Paths” in your Character/Pawn class.