How to calculate Loc offset of 130 units from Forward vector of Enemy?

UE4.27
I searched for answers, didnt find a solution.

Player and Enemy share the same BP. Inside is a SK Reference that is 130X units from the center Capsule.
I am using this SK Reference as a visual to know how close an animation needs to be to the Player/Enemy. (And for some reason the SK Ref needs to be 90 degrees off in Editor, to appear correct in Game. So imagine the 2 meshes are facing each other in Game.)

Then I know where this Offset needs to be, and I put it in a DT > Transform information > Location = vector [130 forward in front of Enemy face, 2 to the side, 0 height change] (so I can grab many different anims and the Loc offset they will need).

So in the game, I have Point A (Enemy/Target’s location). And I have Point B which I want to be [130, 2, 0] units from Point A (the Loc that I grab from the DT).

But if Enemy is rotated 95 degrees in game (World location) = a new Point C that I need to solve for, then I need Point B to be rotated 95. What is the math for this?

  • It’s confusing because Im not sure which axis is forward for the Enemy/character in game, vs the SK Ref axis that is using 130 X units, in the Editor setup. Thank you.

  • Also I cant CastTo Enemy BP to get SK Ref > Get live location - because I am Casting to generic Character and Player/Enemy share the same BP.

  • I tried CastTo default Character BP > try to Get the SK ref that I know is inside Enemy child BP, but the editor doesnt find it.

  • But I do not want to rely on the live SK Ref that is in Enemy BP, with correct live location/rotation, because: avoid CastTo. And each Ref location will be different for each Anim > DT row.


PS the tut video is here. But my question is adding to it/different than his tut. But Im linking for others to find since I couldnt when I originally looked.
[How to sync 2 animation locations]

With the default skeleton yes, it’s a bit odd they don’t use the correct forward axis in the first place. This is just a local rotation of a single component in your actor, which you generally won’t be dealing with in absolute space. You can use the Actor rotation in an absolute world vector with a “GetActorRotation” node. There is also a FindLookAtRotation node which gives you a rotation vector for an actor standing at position A looking at position B. This makes it easier to get a rotation for character A looking towards character B and B towards A. You could set the rotation of both actors to look towards eachother using the SetActorRotation node during an animation.

Also, for very precise animation syncs between two things you can also think of IK setups, those are used when you need animations to be more flexible such as putting a foot on uneven terrain or a hand on a moving door.

2 Likes

Thanks. I put those nodes in my pic. But I dont know the correct math to use it. Can you help?
(I need to get the forward vector or rotation of the Enemy. Add 130X units forward in its [idk axis - but I need to convert my X to its axis]. And I need the rotation (of 130 units in front, to be correct for where the Enemy is facing in game).

Thanks what I need next. Can you direct me to a good tut for UE4.27? (It seems UE5 has an Animation Warping node.) My biggest problem is if Player runs up to Enemy, the distance will be different each time. And I need to rotate both to each other, to find the Final [in front of Enemy 130 units position] - so that Player has less distance to run to - before playing the dual anims.
Thus an IK sys would help each character to know to step closer or not - to bridge the gap.

You could take a look at the free ALS (advanced locomotion system) plugin for 4 or 5 I believe it has IK for the feet on landscape. It also deals with various rotation vectors (of actor and skeleton) which it shows on-screen through a debug visualization so I think in general this one can really show you around.

There’s a lot going on on that pic with the disconnected nodes. You need the understanding of what each node does.

“GetActorForwardVector” returns a normalized (0 to 1) direction vector over X Y Z. This does not retrieve any data related to location at all, it’s just a direction formatted as 0 to 1. You can multiply a direction with a distance. You can add that result to a location:

Distance = 130 (or retrieve distance between location vector A and B with a node).
GetActorLocation() + (GetActorForwardVector() * Distance)

In that formula GetActorLocation() serves as a start position and the result of (GetActorLocation() + (GetActorForwardVector() * Distance)) as an end position.

Another example which is more direct:

Distance = 130;
AIDirection = AICharacter->GetActorForwardVector();
AILocation = AICharacter->GetActorLocation();
AILookAtPosition = AILocation + (AIDirection * Distance);

PlayerCharacter->SetActorLocation(AILookAtPosition);
PlayerPosition = PlayerCharacter->GetActorLocation();
PlayerDirection = FindLookAtRotation(PlayerPosition, AILocation);
PlayerCharacter->SetActorRotation(PlayerDirection);

Oh yes, to retrieve a distance between two points you need to subtract A from B and take the absolute value of that. Blueprints has an “abs” node somewhere for vectors I believe.

Distance = Abs(PlayerPosition - AILocation);

an Abs node simply removes a minus from a value, making it positive while keeping its “size”. aka (300 == 300) && (Abs(-300) == 300)

1 Like

You really shouldn’t have to calculate lookat and such if you have a reference in the characters. As per the video, if you transfer the ref’s transform to the actor to be animated, it should be looking in the right direction already.

As for getting the reference transform in the first place, it might help to make your own component class for it, and add the new component to any actor you want to interact with. Then instead of casting to any specific class to get it, you can use a Get Component by Class node to grab its info. And on a side note, the ref doesn’t have to be a full skele mesh. Once you decide where it should be, even a box or scene dummy would do the trick since the transform is the important part.

Finally, to get the char into the right position, you could just set the transform as a variable, add another boolean for IsDoingPairedAnim or some such, then each tick you T Interp to the stored transform if the bool is true.

1 Like

Thanks for the replies everyone.
@Roy_Wierer.Seda145 for explaining things about vectors. (And yes my pic was a mess because I was showing other people what I tried incorrectly, but may be related to give you ideas how to solve.)
@illspiritx for the reminder “You really shouldn’t have to calculate lookat”

I knew it was easy, just that I was doing it wrong/ and the wrong order of operations.
So here is what worked for How to calculate the Loc offset of 130 units from Enemy’s *current Rotation (not Forward vector).

  • Note that the code sets the Loc and Rot at the same time, which is glitchy/warps. I’ll need to fix that.
  • I have the Enemy [NPC] rotate to Player first (0.5 sec delay of a Timeline), so that the Ref point attached to Enemy can be calculated before Player runs forward or backwards to be 130 units from Enemy.
  • Edit. I should Calculate how much Enemy needs to rotate to Player, and thus Calc (predict) where Enemy’s Point C should be - before I rotate Enemy - so that I know the Point to place Player - and it’s a better blend/less time delay (visual) to move Enemy and Player at the same time.
1 Like

To minimize the glitchy warp, you could turn on ignore move input, and use a simple moveto (or whichever one it is that doesn’t need ai controller) on player to get them in the 130 unit ballpark while the npc is turning around.

Once roughly in place, start interping to the reference dummy on the target. I did something similar before for lining up furniture animations by sticking this in tick:

As suggested above, my furniture transform var was set by grabbing it from a dummy as the animation starts. Since chairs don’t tend to move, you could get the transform from the target dummy each tick here. If the npc is frozen during the anim, there shouldn’t be much difference with a chair though.

Granted, there can be a bit of slide like this. But that’s usually better than glitching, and probably the easiest way to nudge a character to a precise location if not using motion warping in 5.x. And reacting to the target transform is easier than trying to predict it.

As for the positioning lag, you could cover that up a bit with a short warmup animation loop at the start of the montage then jump to the next section once everyone’s in place-ish.

*Edit to add:

Just had a go at making it work this way,and came up with this.

First make a new bp class based on SceneComponent, add it to player/npc blueprints, and position it where other actor should be.


(I added an arrow to it just to see where it is lol)

Then for event graph, here’s tick, and a sphere trace triggered by a key press.

After setting target actor, it goes to this.

The Stop function call just tell the AI character to Stop Movement and focus the instigator, with Roam clearing focus and starting a random wandering event/timer. A proper implementation would probably use interface messages instead of casts, and blackboard keys or whatever for the AI.

Would also want a fallback if the montage doesn’t finish for some reason, but the core part does the job of aligning the player.

1 Like

Thanks for these pics and the edit. Ive been working on a reply, code improved with seeing your pics. I’m still working on it to post.

  • I found way to make the Player look like he’s walking to the Loc - using Simple Move To Location (instead of Set Loc/Transform, or Move [Capsule Component to] - all those fail for collision and/or the feet don’t animate).
  • More importantly, by using Simple Move to, and if the Player is too close to the Target (and thus needs to turn around and move backwards to be 130 away from Target) - then instead it looks like the Player feet step backwards - while body stays faced to Target = looks better for what I want (since I cannot use UE5 Anim warping Node).
  • I made some vids, will post when finished.

Oh yea, I had meant to add a moveto in there after making the ai stop and focus, but was mostly curious how well the t-interp would stick to a possibly still moving target.

Hi @illspiritx , I got the solution to my question. Then I was making the goal better to give you/the forums a copy (of my Char BP) to help others.

But I’ve been working 8 days to improve it/fix bugs. So I waited before posting.

1) How can I do a Blend Rotation on an Actor without a Timeline node?

(for another part of my code, where if pathing fails, I want the Target to rotate to me but not play the anims. I do use a Timeline Rotation in the main code.)

  • The Interp speed is not working to turn slowly. It half turns at speed 5-10. It only fully turns to face me if speed = 200 (too fast/unrealistic spin).
  • Cons of Timeline: it resets the Track pins/deletes the internal graph points - when I copy/paste from a different project.
  • I want to reuse Timelines that go from 0 to 1. But It needs a set Length in the timeline. Thus I cant reuse Timelines? and set a new time Length?
  1. And should I start a new thread and tag so you see? (Im replying to you to offer the finished Char BP, but it’s not ready yet.)

Hmm, that’s quite a pickle. Instead of a timeline, you could use a Delta rotator node to compare current and desired rotation, absolute and multiply the result against a float from a curve asset, then that feed into the interp speed to handle acceleration. Granted, you’d still have to add the curve as a var to any non-child bp that need the functionality, but probably less painful than rebuilding timelines.

If that doesn’t work (or is too expensive lol), maybe make new topic? I’m honestly not sure of the exact protocol here for partial solved topics…

1 Like

This? (I cant connect an Abs without breaking the Rot to Float Z, which is the only rotation I need.)
It rotates the Target Char, but instantly = no delay, no blend. Whats wrong?

Also what is wrong with my prior pic, and the interp speed doesnt work with Delta time? (I searched and many people have problems with Rinterp not working. But I thought it would be solved by now.) Ty

Btw this is what I have so far.

  1. Collision checks that rotate around the Target, to check if there is a clear space to run to. (The rectangle projections from 130x away from Target - to Target.)
  2. Then player runs to chosen Loc. (The Red line trace to Ground, in the vid.)
  3. Then Target rotates to Player. Then they play the anims with 99.9% correct offset Distance.


2a

2b

These are in slowmo.

  • The problems are sometimes in slowmo, #2 fails.
  • And I tested in another map. If a physics object is in the way, the Player path can glitch. Ill report more when I’m ready to post the code.

Probably need to use add rotation, not set.

Kinda sleepy so brain is slow, but quick and dirty example just spinning default Manny around with the mouse in 3rd person map:

Curve has keys at 10, 45, and 100ish with values of .1, 10, and .5 and it does speed up and slow down while turning. Is kinda fiddly though if turning too fast, so maybe not good idea?

No idea why rinterp wouldn’t like delta time. I pulled it of tick, but it should be the same as getworlddelta?

1 Like