Created
March 8, 2016 15:32
-
-
Save sampattuzzi/0a0ca6971ea505b4f438 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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