Skip to content

Instantly share code, notes, and snippets.

@sampattuzzi
Created March 8, 2016 15:32
Show Gist options
  • Save sampattuzzi/0a0ca6971ea505b4f438 to your computer and use it in GitHub Desktop.
Save sampattuzzi/0a0ca6971ea505b4f438 to your computer and use it in GitHub Desktop.
void UGrabber::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction )
{
Super::TickComponent( DeltaTime, TickType, ThisTickFunction );
// Get player view point this tick
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
OUT PlayerViewPointLocation,
OUT PlayerViewPointRotation
);
//UE_LOG(LogTemp, Warning, TEXT("Viewpoint location: %s, rotation: %s"), *PlayerViewPointLocation.ToString(), *PlayerViewPointRotation.ToString());
const auto LineTraceStartVector = PlayerViewPointLocation;
const auto LineTraceEndVector = LineTraceStartVector + Reach * PlayerViewPointRotation.Vector();
DrawDebugLine(
GetWorld(),
LineTraceStartVector + 10 * FVector::UpVector,
LineTraceEndVector,
FColor(255, 0, 0),
false,
0.f,
0.f,
10.f
);
// Ray-cast out to reach distance
FHitResult HitResult;
GetWorld()->LineTraceSingleByObjectType(HitResult, LineTraceStartVector, LineTraceEndVector, FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody));
// See what we hit
AActor* ActorHit = HitResult.GetActor();
auto* handle = (UPhysicsHandleComponent*)GetOwner()->GetComponentByClass(UPhysicsHandleComponent::StaticClass());
if (ActorHit) {
UE_LOG(LogTemp, Warning, TEXT("Hit something %s"), *ActorHit->GetName());
if (!handle->GrabbedComponent) {
handle->GrabComponent(HitResult.GetComponent(), NAME_None, LineTraceEndVector, true);
}
}
handle->SetTargetLocation(LineTraceEndVector);
UE_LOG(LogTemp, Warning, TEXT("Hit something %s"), *handle->GetClass()->GetName());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment