Reading/capturing a change in the direction

Hello again good people.

I have 2.5D Sidescroller setup and a Crow flying from Targen 1 to Target 2 and backwards. I’ve got a basic flying animation and a turn animation and I want to switch betweet these two, when the Crow is turning it’s direction.
Currently I solved it like that and in that context it works.

However, the crow is able to attack and chase the player, the crow should also turn if it is changing it’s direction during the chase. After the player is ouf of reach, the crow returns to it’s patroling path between Target 1 & 2, with the current setup there is also another problem, that the crow’s turn animaton is offset after it returned from a failed chase.
Is there a way to capture such direction change?

As always thanks in regards for your help.

If you want to detect a change in direction of movement (say an inversion) you need to calculate the location delta vector of your crow on a frame by frame basis (Event Tick) and then do a dot product with the location delta vector at the previous frame.

If the dot product is positive, the crow has continued in the same direction. If the dot product turns negative, then the crow has inverted its flying direction.

In event Tick:

CurrentLocationDelta = GetActorLocation() - PreviousActorLocation

DotProduct = CurrentLocationDelta .* PreviousLocationDelta

PreviousActorLocation = GetActorLocation()

PreviousLocationDelta = CurrentLocationDelta

where .* stands for the DOT product between the two vectors.

Then check if DotProduct >= 0 the crow is flying in the same direction
If DotProduct < 0 the crow has inverted its flying direction

Hope this makes sense, otherwise please ask again.

I do understand the process or rather it makes sense to me.
But I don’t know how I execute it. Is this still makeable only with blueprints? I have never coded before. And I never came across the term; delta vector. Is this like the vector on a specific delta second from the Event Tick?

Yes, you can make it with Blueprints. What I wrote above was just pseudocode. A delta vector is just the difference between two vectors. GetActorLocation returns the current position of the crow as a vector. PreviousActorLocation is a vector variable where you store the previous position of the crow. When you subtract the second from the first, you obtain a delta vector which represent how the crow has moved over the last frame. To subtract the two vector your can use the vector-vector node.

Thanks for the answer. How do I store the Location from the last frame correctly? If I just plug the “SetCurrentLocation” and “SetPreviousLocation” behind each other the delta Vector is = 0. That was always the point where all of my other attempts failed.

Here is how it should look like:

Thanks! Had to customize it a bit, but this approach works!