UPhysicsHandleComponent SetTargetLocation updating slowly?

I’ve been away from Unreal Engine for a while, but with the new engine release I decided to get back into it. I wanted to build something simple to clear off the rust in my programming skills, so I decided to make a Gravity Gun, similar to the one from Half Life 2.

First, I set everything up in C++ and just set the Actor Location directly. This worked, of course, but it allowed the object to phase through other solid objects. So I did some research and found a lot of people trying to recreate this same functionality by using UPhysicsHandleComponent. I attempted to replicate this, and got pretty far, but my implementation wasn’t moving the object quickly enough.

I couldn’t really understand why, so I decided to watch many blueprint tutorials (most of them follow the same procedure) and replicate their exact steps. However, even following their blueprint exactly as they had done, I still encountered this problem.

An example tutorial is: Let's Create Half Life 2! Gravity Gun - Blueprints #12[Unreal Engine 4] - YouTube
At around the 12:10 mark, he demonstrates the Gravity Gun and you can clearly see the object move in sync with the center of his screen, such that there is no delay between when he moves and when the object moves.

However, using the same exact blueprint logic, my implementation looks like this:

You can also see at the top left corner a print statement every time I press the button that is supposed to pick the object up. When the object is stationary, SetTargetLocation does not move the object at all. I have to launch the cube into the air, and only then will SetTargetLocation do something. But you can clearly see after that, that the object does not follow as quickly as the tutorial shows.

The only difference I can immediately think of is the fact that this is UE5 and the tutorials were done in UE4, but other than that I can’t spot any other differences.

Is anyone else having this problem?

1 Like

UE4 uses PhysX, whereas UE5 uses Chaos for physics. And they are quite different. I tried migrating a project from 4.27 to 5.0, which uses physics handle, and I was unable to set it up so that it would work as smoothly as in 4.27. Interpolation issue that you experience is only one of a number of issues.
In this case you can mitigate it by increasing Linear Stiffness of the Physics Handle; I found that a value around 8500-10000 works about right.

1 Like

Thanks for such a quick reply!

I changed the Linear Stiffness and that definitely helped, but the values seemed different for me. In the 10,000 range, the object basically didn’t move. The values that worked well were in the 25-35 range, and any lower than this would make the object bounce side to side.

However, the objects still do not change their location unless they are already moving. Even if the object was previously moving and then becomes stationary, SetTargetLocation has no effect unless the object has some non-zero velocity.

1 Like

UPDATE:

I added an AddImpulse as a trick to see if that will get the SetTargetLocation to update the location of the object without having to manually move it with my character’s body or projectile, which did work, but the weird thing is that I set the FVector to have 0 in every component. It’s literally just an FVector::ZeroVector. It is not actually adding an impulse to the object, but it seems like it “notifies” it in some way to start updating from SetTargetLocation.

And now I’m basically running into the reverse of this problem. If I continue holding the Gravity Gun button, but stop moving my camera so that the object becomes still, I can look around and even let go of the button and the object will just float in the air.

I bound the Gravity Gun button to print whenever it has been released. You can see in the video that a lack of movement eventually stops SetTargetLocation from working, even while the button is still being held.

It is almost as if the engine does not like update objects using UPhysicsHandleComponent if they are not already being acted upon in some way.

1 Like

Try this at the start of the pickup function:

PhysicsHandleRef->GetGrabbedComponent()->WakeRigidBody();

Do the same after release if the grabbed component stays floating.

1 Like

I tried this out and it now works in place of the AddImpulse.

The object still continues to float if left alone for long enough, but only after releasing the button, not when moving the camera. It also seems to happen less frequently.

1 Like

It will go to “sleep” even If you grab the component and don’t move the mouse for a couple of seconds without releasing.

Try this in tick to know when it goes to sleep:

if (PhysicsHandle->GrabbedComponent)
{
		UE_LOG(LogTemp,Warning,TEXT("Physics State: %s"), PhysicsHandle->GetGrabbedComponent()->RigidBodyIsAwake() ? TEXT("Awake.") : TEXT("Asleep."));
}

WakeRigidBody() will wake it up for like 1 second or so until it becomes motionless and sleeps again.

2 Likes

I used an if statement to check if IsAnyRigidBodyAwake, and if true, it wakes the body. The object no longer hovers when implemented in C++, but not with Blueprints. I will keep experimenting with both versions.

1 Like