3

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

amb9800
  • 365
  • 2
  • 5
  • 14
  • 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 direction` – Ruzihm Jun 25 '19 at 17:09

2 Answers2

10

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.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
Bas in het Veld
  • 1,271
  • 10
  • 17
  • This is a great answer. I didn't think about InverseTransformLocation. Is there a meaningful difference between [RotationFromXVector](https://api.unrealengine.com/INT/BlueprintAPI/Math/Rotator/RotationFromXVector/index.html) and [MakeRotFromX](https://api.unrealengine.com/INT/BlueprintAPI/Math/Rotator/MakeRotfromX/index.html)? I can't find the former by browsing through the documentation, only searching for it. – Ruzihm Jun 25 '19 at 20:58
  • Thanks- this is indeed quite elegant and works well! – amb9800 Jun 26 '19 at 06:15
  • 2
    @Ruzihm 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. – Bas in het Veld Jun 26 '19 at 11:32
0

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.

NobodySomewhere
  • 2,997
  • 1
  • 16
  • 12