Why use GetComponent

Why are we using GetComponent() insted of GetActor()? Aren’t we getting the Actor which is being hit by the player reach in the following code:
FHitResult HitResult = GetFirstPhysicsBodyInReach();
UPrimitiveComponent* HitObject = HitResult.GetComponent();

Yes and no. The FHitResult would contain information about the line trace. That code there is getting the component of the actor that was hit. It’s needed for GrabComponentAtLocation.

That means that it tells the point of impact on the line trace, then what dose it contain the Actor or its components if both then why do we have to Grab by Component not by actor.

FHitResult stores several things about the hit

FHitResult | Unreal Engine Documentation

Because a component is more useful. Consider an actor with multiple static mesh components, it would be reasonable to be able to grab the specific component you want so the physics behaves as you would expect.

Now Epic could have added a GrabActor that just uses the root component but they didn’t.

Ok I Get it now Thx for the help.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms