FRotator off by 90 or wrong axis?

I am taking an example from a diffrent project and re-creating it in my own game. In the original working project, there is a gun that rotates around the hand correctly and these are some of the correct rotation values:

P=0.000000 Y=-48.702690 R=-8.323224
P=0.000000 Y=-49.789928 R=-8.552868
P=-0.000007 Y=-50.883415 R=-8.711617
P=-0.000007 Y=-52.022709 R=-8.827152
P=-0.000007 Y=-53.098286 R=-8.840174
P=0.000000 Y=-54.122597 R=-8.782237
P=0.000000 Y=-55.130390 R=-8.710796
P=0.000000 Y=-56.111393 R=-8.561297
P=0.000007 Y=-57.071911 R=-8.353694
P=0.000000 Y=-58.066284 R=-8.080949
P=0.000007 Y=-58.996971 R=-7.765880
P=0.000000 Y=-60.021267 R=-7.374957
P=0.000000 Y=-61.099659 R=-6.955542
P=0.000000 Y=-62.155025 R=-6.534186
P=-0.000007 Y=-63.222946 R=-6.109061
P=-0.000007 Y=-64.318413 R=-5.663557
P=-0.000007 Y=-65.328362 R=-5.252420
P=-0.000007 Y=-66.379761 R=-4.831179
P=-0.000007 Y=-67.402679 R=-4.408598
P=0.000000 Y=-68.367493 R=-4.024447

In my own project, I print out the same rotation values the same way its done in the example but the results look like this and produce an incorrect rotation:

P=7.775964 Y=-7.446252 R=-0.000001
P=7.824084 Y=-7.487617 R=-0.000001
P=7.818578 Y=-7.518079 R=-0.000001
P=7.850033 Y=-7.517104 R=-0.000001
P=7.858570 Y=-7.491702 R=-0.000001
P=7.864113 Y=-7.506747 R=-0.000001
P=7.866255 Y=-7.553430 R=-0.000001
P=7.897817 Y=-7.592583 R=-0.000001
P=7.916775 Y=-7.657037 R=-0.000001
P=7.922338 Y=-7.749585 R=-0.000001
P=7.989968 Y=-7.808784 R=-0.000001
P=8.000801 Y=-7.914917 R=-0.000001
P=7.993641 Y=-7.998132 R=-0.000001
P=7.993693 Y=-8.040081 R=-0.000001
P=7.861572 Y=-8.544514 R=-0.000001
P=7.832154 Y=-8.645342 R=-0.000001
P=7.791457 Y=-8.710664 R=-0.000001
P=7.802832 Y=-8.731378 R=-0.000001
P=7.763756 Y=-8.795066 R=-0.000002
P=7.734653 Y=-8.790546 R=-0.000001
P=7.715140 Y=-8.758606 R=-0.000001
P=7.686746 Y=-8.767592 R=-0.000001
P=7.592921 Y=-8.746538 R=-0.000001
P=7.560185 Y=-8.724867 R=-0.000001
P=7.504443 Y=-8.743661 R=-0.000001
P=7.429923 Y=-8.720572 R=-0.000001
P=7.396094 Y=-8.722472 R=-0.000001
P=7.371034 Y=-8.729496 R=-0.000001
P=7.331400 Y=-8.721493 R=-0.000001
P=7.353291 Y=-8.695507 R=-0.000001
P=7.358566 Y=-8.671263 R=-0.000001
P=7.356328 Y=-8.624143 R=-0.000001
P=7.369827 Y=-8.545854 R=-0.000001
P=7.372361 Y=-8.504041 R=-0.000001

As you can see, it looks like something is slightly off with the math. I first tried to simply switch the pitch and the roll but that didn’t really help too much. I think the answer might be in using UKismetMathLibrary::ComposeRotators() to combine the rotation by 90 somewhere or rotate the rotator?

Edit: Here is the code used to calculate the rotation for the gun when the second hand grabs it (second hand is not attached but the first one is attached).

void ATwoHandGunWeapon::Tick(float deltaTime)
{
	FVector handleLocalPos;
	FVector gripLocalPos;
	FRotator gunLookAtRotation;

	Super::Tick(deltaTime);

	//IF the grip is being touched aswell, account for the rotation.
	if (mySecondaryHand != NULL && myGripComponent != NULL && myFirePointComponent != NULL && myRootComponent != NULL)
	{
		handleLocalPos = UKismetMathLibrary::InverseTransformLocation(this->GetActorTransform(), myPrimaryHand->GetControllerLocation());
		gripLocalPos = UKismetMathLibrary::InverseTransformLocation(this->GetActorTransform(), mySecondaryHand->GetControllerLocation());

		gunLookAtRotation = UKismetMathLibrary::FindLookAtRotation(handleLocalPos, gripLocalPos);

		//Combines two rotators together.
		//gunLookAtRotation = UKismetMathLibrary::ComposeRotators(FRotator(0.0f, 0.0f, -90.0f), gunLookAtRotation);

		UE_LOG(LogTemp, Error, TEXT("after compose gunLookAtRotation:%s"), *(gunLookAtRotation.ToString()));

		myRootComponent->SetRelativeRotation(gunLookAtRotation);
	}
}

Seems like they might be local rotations so it could be hard to tell from just the numbers. Is this gun being attached to a socket?

I’m attaching the gun to my right hand, its when I grab with my left hand that I do the rotation. I dont attach the gun to the left hand.
These are relative rotations, I set the relative rotation of the item.

Check the skeletal mesh of the character in both projects and make sure the right hand sockets have the same rotation.

In this case, The hands are USkeletalMeshComponents so I’m not using the character mesh. I checked the skeletal mesh component rotation in my project and it is 0. In the example project that is working, two static mesh spheres are being used to represent the hands. I checked the rotation of the static mesh component of the hand and it was 0.

Well when we use a full character mesh we rotate it 90 degrees on the Z axis so you could try using Delta(Rotator) to subtract 90 degrees or Compose to add in negative 90. Or maybe you need to add it in. It’s really hard to tell with what you’ve provided. You could post some code and screenshots and maybe I’d have a better idea.

Interesting, I tried doing almost exactly that. I’ll post some code that calculates the the rotation I’m talking about.

Stop using kismet. Those are base functions for blueprint nodes. What you should be writing is the code you see inside of those functions.

Calling the function directly vs doing whats in the function; I don’t see how that will change anything though?

You would type a lot less. And you would be relying on the base math classes which means your code runs faster. No it doesn’t fix the problem your asking about. Which is why I made it a comment. But you really should learn how to use FMath, FVector, FRotator…

Ok, I figured out the issue and it will take a little bit of explaining.

The two handed gun I am rotating is a series of static meshes that are parented together like so:

232922-capture.png

When I originally posted this, I was grabbing the handle with the primary hand which attached and worked correctly. But when I grabbed with the secondary hand to rotate the gun, it rotated the wrong way (my original problem with it off by 90 somewhere). This is because you are not supposed to rotate around the handle but actually rotate around the GunRoot.

The correct way to do this is to attach your hand to the handle but then Rotate the GunRoot Component and not the handle. If you try to do this out of the box the gun wont rotate and this is because the GunRoot is a USceneComponent and needs to be attached to something grabbable and moveable (UPrimitiveComponent). Below is how the GunRoot is attached to the handle. Once I had this in place, I was able to pick up the gun and rotate it correctly.

void ATwoHandGunWeapon::OnPlayerReceive(ABaseHand* owningHand)
{
	UPrimitiveComponent* handlePrimitiveComponent = NULL;

	//Attachment is diffrent for the two handed weapon since rotation is based on the root.
	//Unfortunattly the root is a scene component so it needs to be attached to a moveable part.
	if (owningHand != NULL)
	{
		handlePrimitiveComponent = Cast<UPrimitiveComponent>(myHandleComponent);
		if (handlePrimitiveComponent != NULL)
		{
			handlePrimitiveComponent->SetSimulatePhysics(false);

			handlePrimitiveComponent->AttachToComponent(myGunRootComponent, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::KeepWorld, true));
		}

		this->AttachToComponent(owningHand->GetVRHandMesh(), FAttachmentTransformRules(EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::KeepWorld, true));
	}
}

The above code was the key to get it working because it attaches the GunRoot to the handle. I thought this was already done automatically because of the hierarchy in the first picture but apparently not, the results don’t lie.