Getting yaw angle between two actors - Unreal Blueprints
Asked Answered
P

2

5

I'm working on a VR game in Unreal Engine 4 using Blueprints.

I want to calculate the (yaw) angle the user needs to turn his/her gun (whose direction is determined via the position of the motion controllers) in order to be pointing towards the target.

I figure this might be the way to do it:

  • Subtract the location of the target from the location of the gun
  • Get the yaw component of that as a vector pointing from the gun as origin
  • Subtract the current yaw of the gun direction from that yaw component to get the yaw angle the user needs to turn to get to the target

Except I'm not quite sure how to execute that. I've been experimenting (as seen in the screenshot below), but not doing the correct operations. Any thoughts?

Thanks!

Unreal Engine 4 screenshot

Purgation answered 25/6, 2019 at 7:39 Comment(1)
Do the subtraction target location - firearm location then normalize. Find the yaw of that normalized value to create yaw of target direction Then, find yaw of the firearm's forward vector. Then, subtract again yaw of target direction - yaw of firearm's forward vector to produce yaw delta from firearm's forward to target directionEph
F
12

A more elegant and robust solution is to use the gun actor's world transform to calculate the relative rotation to the object:

  1. Get the gun's world transform. The rotation should point in the forward vector direction. You can make a transform with its location and forward vector, but likely the component transform will work.
  2. Use the operation InverseTransformLocation on this transform, with the target's location as other parameter. This creates a vector that is the target's location in the gun's space
  3. Get the rotation of this vector with the RotationFromXVector operation.

This rotator contains the correct yaw, but also pitch. And it will also work when your objects are rotated in space arbitrarily, or your objects become children of even more actors.

Finnie answered 25/6, 2019 at 19:50 Comment(3)
This is a great answer. I didn't think about InverseTransformLocation. Is there a meaningful difference between RotationFromXVector and MakeRotFromX? I can't find the former by browsing through the documentation, only searching for it.Eph
Thanks- this is indeed quite elegant and works well!Purgation
@Eph Honestly, RotationFromXVector simply calls Vector.Rotation() internally. MakeRotFromX is much more elaborate, using a Rotation matrix. I don't see the use of that in blueprints, so I'm guessing they exposed it mainly for completeness.Finnie
T
1

This is how I do it: Finding angle between two actors with 'Find look at rotation'

'Find look at rotation' is a function from 'Kismet Math Library' (unreal math library). It finds world rotation for an object at Start location to point at Target location.

Triumphal answered 17/2, 2022 at 11:16 Comment(1)
Delta Rotator + Break Rotator was exactly what I needed. Thank you!Imminence

© 2022 - 2024 — McMap. All rights reserved.