Camera Rotation with moving turret

Hello, So I am making a tank game and I setup the turret to have to same rotation at the camera so that it moves with your mouse. However, whenever I turn my tank the turret moves with it and doesn’t follow the camera. When I move the camera again the turret moves but its offset from the camera. Also my camera is attached to a spring arm if that matters.

First issue I see is that you take world space rotation of camera and then use it as relative rotation for turret, don’t do that - rotations and positions need to be transformed into a proper space before they can be used together. Or do your calculations only in world space or only in local/relative space.
I don’t have editor in front of me, check if there is GetLocalRotation or GetRelativeRotation for the camera and use that instead of GetWorldRotation.
This approach will work only if both turret and camera are attached to the same parent and don’t have any offset in between.

More generic solution take more steps but can be wrapped into a single function, this function can be used to rotate any amount of turrets on tank into any arbitrary direction.

  1. take world space X vector of camera (there is a node like this)
  2. construct world space Transform of turret without rotation (which is done by making rotator with only turret relative position plugged in and combine this rotator with turret’s parent world space transform by using Compose Transforms)
  3. now you can inverse Transform (InverseTransformDirection) vector from step 1 by Transform from step 2, this gives you a direction vector camera is looking at in local space of turret before rotation
  4. find LookAtRotation using vector from step 3, this will be new turret local rotation

By replacing camera X vector with some direction in space, like for example there is target somewhere in the world and you select it, you can calculate direction to target as TargetPosition - TankPosition, feed this vector into the function and you get rotation of turret towards this target.

Thanks! It works perfectly now :slight_smile: