Portal style grabbing (rotate with you and make upright)

I wanted the grabbed objects to rotate with you as you are holding them, as well as return to the upright position when you grab them. This way you can flip a table and chairs in anger and then easily set them back up neatly.

In Grabber.h create these private variables:

// Used for making grabbed item always upright and rotate with you
float GrabbedCapturedYaw = 0.0f; // Yaw of the Grabbed item is captured here the moment it's grabbed.
float PlayerCapturedYaw = 0.0f; // Yaw of the Player is captured here the moment you grab something.

In Grabber.cpp add these lines to the Grab function to essentially take a “snapshot” of these values at the moment you grab and stores them in the above variables mentioned:

GrabbedCapturedYaw = ActorHit->GetActorRotation().Yaw;
PlayerCapturedYaw = PlayerViewPointRotation.Yaw;

Then in Grabber.cpp in the TickComponent:

	//If physics handle is attached
	if (PhysicsHandle->GrabbedComponent)
	{
		float PlayerYaw = PlayerViewPointRotation.Yaw; // The player's Yaw is updated here.
		float NewYaw = (PlayerYaw-PlayerCapturedYaw) + GrabbedCapturedYaw; // Your changes in rotation are added to the grabbed object.
		
		//Move the object that we're holding
		PhysicsHandle->SetTargetLocation(LineTraceEnd);
		PhysicsHandle->SetTargetRotation(FRotator(0.0f, NewYaw, 0.0f)); // Grabbed object is made up-right and it's Yaw rotates with the player's.
...
2 Likes

Does this mean if you knock a table over before grabbing it, you will always grab it wrong? Wouldn’t it be better to assign this value at runtime somehow?

1 Like

Privacy & Terms