Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1.  
  2. // Sets default values for this component's properties
  3. UGrabber::UGrabber()
  4. {
  5. // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
  6. // off to improve performance if you don't need them.
  7. bWantsBeginPlay = true;
  8. PrimaryComponentTick.bCanEverTick = true;
  9. }
  10.  
  11.  
  12. // Called when the game starts
  13. void UGrabber::BeginPlay()
  14. {
  15. FindPhysicsHandleComponent();
  16. SetupInputComponent();
  17. }
  18.  
  19. /// Look for attached Physics Handle
  20. void UGrabber::FindPhysicsHandleComponent()
  21. {
  22. PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
  23. if (PhysicsHandle)
  24. {
  25. // Physics handle is found
  26. }
  27. else
  28. {
  29. UE_LOG(LogTemp, Error, TEXT("%s missing physics handle component"), *GetOwner()->GetName())
  30. }
  31. }
  32.  
  33.  
  34. /// Look for attached Input Component (only appears at run time)
  35. void UGrabber::SetupInputComponent()
  36. {
  37. InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
  38. if (InputComponent)
  39. {
  40. UE_LOG(LogTemp, Warning, TEXT("Input component found"))
  41. /// Bind the input axis
  42. InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
  43. InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release);
  44. }
  45. else
  46. {
  47. UE_LOG(LogTemp, Error, TEXT("%s missing input component"), *GetOwner()->GetName())
  48. }
  49. }
  50.  
  51.  
  52. // Called every frame
  53. void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
  54. {
  55. Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
  56.  
  57. // if the physics handle is attached
  58. // move the object that we're holding
  59. }
  60.  
  61.  
  62. void UGrabber::Grab() {
  63. UE_LOG(LogTemp, Warning, TEXT("Grab pressed"))
  64.  
  65. /// LINE TRACE and see if we reach any actors with physics body collision channel set
  66. GetFirstPhysicsBodyInReach();
  67.  
  68. /// If we hit something then attach a physics handle
  69. // TODO attach physics handle
  70. }
  71.  
  72. void UGrabber::Release()
  73. {
  74. UE_LOG(LogTemp, Warning, TEXT("Grab released"))
  75. // TODO release physics handle
  76. }
  77.  
  78. const FHitResult UGrabber::GetFirstPhysicsBodyInReach()
  79. {
  80. /// Get player view point this tick
  81. FVector PlayerViewPointLocation;
  82. FRotator PlayerViewPointRotation;
  83. GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
  84. OUT PlayerViewPointLocation,
  85. OUT PlayerViewPointRotation
  86. );
  87.  
  88. FVector LineTraceEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;
  89.  
  90. /// Setup query parameters
  91. FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner());
  92.  
  93. /// Line-trace (AKA ray-cast) out to reach distance
  94. FHitResult Hit;
  95. GetWorld()->LineTraceSingleByObjectType(
  96. OUT Hit,
  97. PlayerViewPointLocation,
  98. LineTraceEnd,
  99. FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
  100. TraceParameters
  101. );
  102.  
  103. /// See what what we hit
  104. AActor* ActorHit = Hit.GetActor();
  105. if (ActorHit)
  106. {
  107. UE_LOG(LogTemp, Warning, TEXT("Line trace hit: %s"), *(ActorHit->GetName()))
  108. }
  109.  
  110. return FHitResult();
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement