/** * Get location and rotation of camera. Sets players view point to camera location and rotation. * Sets where the trace will start, which direction, and where it ends. Traceparms setup to return details of whats collided. * Traces a ray against the world, returning the first blocking hit. * * @params location to store hit info, distance of trace ray, the collision channel * @return return the hit actor. **/ bool APlayerCharacter::TraceFromSelf(FHitResult& OutResult, const float TraceDistance, ECollisionChannel const CollisionChannel) { if (Controller) { FVector CameraLoc; FRotator CameraRot; Controller->GetPlayerViewPoint(CameraLoc, CameraRot); //Location to cast ray (from player) FVector const BeginTrace = CameraLoc; FVector const ShootDir = CameraRot.Vector(); FVector const TraceEnd = BeginTrace + ShootDir * TraceDistance; FCollisionQueryParams TraceParms(FName(TEXT("TraceFromSelf")), true, this); //returns what collided bool bHitReturned = false; //to determine if anything hit UWorld* const World = GetWorld(); if (World) { bHitReturned = World->LineTraceSingleByChannel(OutResult, BeginTrace, TraceEnd, CollisionChannel, TraceParms); } TraceParms.bTraceAsyncScene = true; //Whether we should perform the trace in the asynchronous scene.Default is false. TraceParms.bReturnPhysicalMaterial = false; //Only fill in the PhysMaterial field TraceParms.bTraceComplex = true; //Whether we should trace against complex collision return bHitReturned; } return false; //if there is no controller, nothing traced. }
const FHitResult UGrabber::GetFirstPhysicsBodyInReach() { // Line trace FHitResult HitResult; FCollisionQueryParams TraceParms(FName(TEXT("")), false, GetOwner()); GetWorld()->LineTraceSingleByObjectType(OUT HitResult, GetReachLineStart(), GetReachLineEnd(), FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody), TraceParms); return HitResult; }