Grab Component at any Reach

Using the FVector::Size(); function, I was able to enable the player to grab physics bodies within any range of the set reach (so if the reach was 10,000 centimeters, he can grab anything in that range).

I only modified the Grab and the Tick function to do this, along with making a couple variables used in the lecture to within the scope of all functions.(declared in header instead of in the fuction) Those variables were:

// "Grabber.h":
FHitResult HitResult; 
FVector PlayerViewPointLocation;

In the lecture HitResult was simply called Hit. The HitResult variable i am using will be used in both functions but not in the GetPhysicsBody() function from the lecture, where it will still use a local version of HitResult called Hit; it will use the global PlayerViewPointLocation variable however , but this function is unaltered in any way besides this one difference.

I also had to store the maginitude from the vector size result in the header file, called GrabbedActorDistance:

// "Grabber.h":
float GrabbedActorDistance;

Fetching the size of a vector will give you its magnitude which is essentially the size in centimeters if you were to use it as a length multiplier, just as you would use the Reach variable to jet out 500 centimeters in front of the player.(or use it as a bullet speed for that matter)

The two altered functions from the lecture(not counting the get physics body one), are:

// "Grabber.cpp":
void UGrabber::ActionGrab() // i called this ActionGrab , it is the same as Grab
{
	HitResult = FindPhysicsBodyinRange();  // this is the same function as GetPhysicsBody from the lecture
	auto ComponentToGrab = HitResult.GetComponent();
	if (ComponentToGrab!=nullptr) {
		physxHandleComp->GrabComponentAtLocationWithRotation(
			ComponentToGrab,
			NAME_None,
			ComponentToGrab->GetOwner()->GetActorLocation(),
			ComponentToGrab->GetOwner()->GetActorRotation());

		// this is what was altered in this function, you must fetch the GrabbedActorDistance on ActionGrab,
		//   this way it will remain the same for every tick
		FVector HitActorLocation = HitResult.GetActor()->GetActorLocation();
		FVector ActorDifference = (HitActorLocation - PlayerViewPointLocation);
		GrabbedActorDistance= ActorDifference.Size();
	}
}	

// The other thing I changed was the way the tick function from the lecture works
// It uses its own local version of PlayerViewPointLocation, not to be confused with the one
// in ActionGrab();, which uses a public/global one shared with FindPhysicsBodyinRange();

void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
	
	FVector PlayerViewPointLocation;
	FRotator PlayerViewPointRotation;
	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
		OUT PlayerViewPointLocation, OUT PlayerViewPointRotation);
	FVector PlayerDirection = PlayerViewPointRotation.Vector();
	FVector LineTraceEnd;
	
	
	// this is where the function changes,
	//  remember this is using the global HitResult variable
	if (HitResult.GetActor()) {
	
		// the line trace is calculated using the public GrabbedActorDistance float,
		// instead of the Reach float used in the lecture.
		// This float is modified in the ActionGrab() event.
		LineTraceEnd = (PlayerViewPointLocation + PlayerDirection*GrabbedActorDistance);
		physxHandleComp->SetTargetLocation(LineTraceEnd);
	}
}

And so yea it works xD. It was a little confusing until i realized i needed to make a few variables public, then modify the GrabbedActorDistance in the ActionGrab function, instead of in tick. (if you modify the GrabbedActorDistance in tick, then you will loose the ability to manipulate the physics body object once you hover off it, and it also will not follow you forward and backward, since the distance between you and the physics object would be recalculated every frame, allowing the actor to sit in the same spot instead of being told to thrust the amount of magnitude in front of you.


You’re just gonna have to trust me that i’m picking it up :smiley: (from any distance)

A few more functions and this could operate the same as the physics gun from Gmod. All it needs is a rotate key and a move forward, back key, then a visual representation

Privacy & Terms