diff --git a/RoomEscape/Source/RoomEscape/Grabber.cpp b/RoomEscape/Source/RoomEscape/Grabber.cpp index a77bd38..f636a68 100644 --- a/RoomEscape/Source/RoomEscape/Grabber.cpp +++ b/RoomEscape/Source/RoomEscape/Grabber.cpp @@ -35,7 +35,20 @@ void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorCompone // if physics handle is attached // move object holding // do nothing + /// Get player View point in this tick + FVector PlayerViewpointLocation; + FRotator PlayerViewpointRotation; + GetWorld()->GetFirstPlayerController( + )->GetPlayerViewPoint( + OUT PlayerViewpointLocation, + OUT PlayerViewpointRotation + ); + FVector LineTraceEnd = PlayerViewpointLocation + PlayerViewpointRotation.Vector() * Reach; + + if (PhysicsHandle->GrabbedComponent) { + PhysicsHandle->SetTargetLocation(LineTraceEnd); + } @@ -47,9 +60,20 @@ void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorCompone void UGrabber::Grab() { UE_LOG(LogTemp, Warning, TEXT("GRABBING!")); // LINE TRACE and Try and reach any actors with physics body collision channel set - GetFirstPhysicsBodyInReach(); + auto HitResult = GetFirstPhysicsBodyInReach(); + auto ComponentToGrab = HitResult.GetComponent(); + auto ActorHit = HitResult.GetActor(); + // if we hit something attach physics handle // TODO: attach physics handle + if (ActorHit) { + PhysicsHandle->GrabComponent( + ComponentToGrab, + NAME_None, + ComponentToGrab->GetOwner()->GetActorLocation(), + true //allow rotation + ); + } } @@ -57,6 +81,8 @@ void UGrabber::Grab() { void UGrabber::Release() { UE_LOG(LogTemp, Warning, TEXT("Released!")); //TODO: Release physics handle + PhysicsHandle->ReleaseComponent(); + } // Look for attached physics handle diff --git a/RoomEscape/Source/RoomEscape/Grabber.h b/RoomEscape/Source/RoomEscape/Grabber.h index 6dc6a15..c662110 100644 --- a/RoomEscape/Source/RoomEscape/Grabber.h +++ b/RoomEscape/Source/RoomEscape/Grabber.h @@ -37,4 +37,5 @@ private: // Return hit for first physics body in reach FHitResult GetFirstPhysicsBodyInReach() const; + };