Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------.CPP FILE------------------------
- #include "Grabber.h"
- #include "Engine/World.h"
- #include "DrawDebugHelpers.h"
- #define OUT
- // Sets default values for this component's properties
- UGrabber::UGrabber()
- {
- // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
- // off to improve performance if you don't need them.
- PrimaryComponentTick.bCanEverTick = true;
- Reach = 100.f;
- // ...
- }
- // Called when the game starts
- void UGrabber::BeginPlay()
- {
- Super::BeginPlay();
- UE_LOG(LogTemp, Warning, TEXT("Grabber reporting for duty!"));
- }
- // Called every frame
- 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
- );
- ///TODO Log out to test
- FVector LineTraceEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;
- //Draw a red trace in the world to visualize
- DrawDebugLine(GetWorld(), PlayerViewPointLocation, LineTraceEnd, FColor(255, 0, 0), false, 0.f, 0.f, 10.f);
- ///Setup query parameters
- FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner());
- ///Ray-cast out to reach distance
- FHitResult hit;
- GetWorld()->LineTraceSingleByObjectType(
- OUT hit,
- PlayerViewPointLocation,
- LineTraceEnd,
- FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
- TraceParameters
- );
- AActor* ActorHit = hit.GetActor();
- if (ActorHit)
- {
- UE_LOG(LogTemp, Warning, TEXT("%s"), *(ActorHit->GetName()));
- }
- //See what we hit
- }
- ------------------------HEADER FILE------------------------
- // Fill out your copyright notice in the Description page of Project Settings.
- #pragma once
- #include "CoreMinimal.h"
- #include "Components/ActorComponent.h"
- #include "Grabber.generated.h"
- UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
- class BUILDINGESCAPE_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:
- UPROPERTY(VisibleAnywhere)
- float Reach;
- };
Advertisement
Add Comment
Please, Sign In to add comment