// Copyright Patrick Werner 2019 #pragma once #include "CoreMinimal.h" #include "Components/ActorComponent.h" #include "PhysicsEngine/PhysicsHandleComponent.h" #include "Components/InputComponent.h" #include "Grabber.generated.h" struct PlayerViewportTransform { FVector Location; FRotator Rotation; }; UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) class UE4CPP_SECTION_03_API UGrabber : public UActorComponent { GENERATED_BODY() public: // Sets default values for this component's properties UGrabber(); protected: // Called when the game starts virtual void BeginPlay() override; public: // Called every frame virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; private: // how far ahead of the player can we reach in cm float Reach = 150.f; UPhysicsHandleComponent* PhysicsHandle = nullptr; UInputComponent* InputComponent = nullptr; // gets relevant player viewport transform data PlayerViewportTransform GetPlayerViewportTransformData() const; // returns current start of reach line FVector GetLineTraceStart() const; // returns current end of reach line FVector GetLineTraceEnd() const; // ray cast and grab what's in reach void Grab(); // called when greb is released void Release(); // find PhysicsHandle void FindPhysicsHandleComponent(); // find InputComponent void FindInputComponent(); // bind user inputs to methods void BindInputs() const; // Return hit for first physics body in reach FHitResult GetFirstPhysicsBodyInReach() const; };