Guest User

grabber c++ code

a guest
Oct 23rd, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. ------------------------.CPP FILE------------------------
  2.  
  3. #include "Grabber.h"
  4. #include "Engine/World.h"
  5. #include "DrawDebugHelpers.h"
  6.  
  7. #define OUT
  8.  
  9. // Sets default values for this component's properties
  10. UGrabber::UGrabber()
  11. {
  12.     // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
  13.     // off to improve performance if you don't need them.
  14.     PrimaryComponentTick.bCanEverTick = true;
  15.     Reach = 100.f;
  16.  
  17.     // ...
  18. }
  19.  
  20.  
  21. // Called when the game starts
  22. void UGrabber::BeginPlay()
  23. {
  24.     Super::BeginPlay();
  25.     UE_LOG(LogTemp, Warning, TEXT("Grabber reporting for duty!"));
  26.    
  27. }
  28.  
  29.  
  30. // Called every frame
  31. void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
  32. {
  33.     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
  34.  
  35.     //Get player view point this tick
  36.     FVector PlayerViewPointLocation;
  37.     FRotator PlayerViewPointRotation;
  38.     GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
  39.         OUT PlayerViewPointLocation,
  40.         OUT PlayerViewPointRotation
  41.     );
  42.  
  43.     ///TODO Log out to test
  44.  
  45.     FVector LineTraceEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;
  46.  
  47.         //Draw a red trace in the world to visualize
  48.         DrawDebugLine(GetWorld(), PlayerViewPointLocation, LineTraceEnd, FColor(255, 0, 0), false, 0.f, 0.f, 10.f);
  49.  
  50.     ///Setup query parameters
  51.         FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner());
  52.  
  53.     ///Ray-cast out to reach distance
  54.         FHitResult hit;
  55.  
  56.         GetWorld()->LineTraceSingleByObjectType(
  57.             OUT hit,
  58.             PlayerViewPointLocation,
  59.             LineTraceEnd,
  60.             FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
  61.             TraceParameters
  62.         );
  63.          
  64.         AActor* ActorHit = hit.GetActor();
  65.         if (ActorHit)
  66.         {
  67.             UE_LOG(LogTemp, Warning, TEXT("%s"), *(ActorHit->GetName()));
  68.         }
  69.     //See what we hit
  70.  
  71. }
  72.  
  73. ------------------------HEADER FILE------------------------
  74.  
  75. // Fill out your copyright notice in the Description page of Project Settings.
  76.  
  77. #pragma once
  78.  
  79. #include "CoreMinimal.h"
  80. #include "Components/ActorComponent.h"
  81. #include "Grabber.generated.h"
  82.  
  83.  
  84. UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
  85. class BUILDINGESCAPE_API UGrabber : public UActorComponent
  86. {
  87.     GENERATED_BODY()
  88.  
  89. public:
  90.     // Sets default values for this component's properties
  91.     UGrabber();
  92.  
  93. protected:
  94.     // Called when the game starts
  95.     virtual void BeginPlay() override;
  96.  
  97. public:
  98.     // Called every frame
  99.     virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
  100.  
  101. private:
  102.  
  103.     UPROPERTY(VisibleAnywhere)
  104.     float Reach;
  105.    
  106. };
Advertisement
Add Comment
Please, Sign In to add comment