Created
August 7, 2018 20:04
-
-
Save MihaMarkovi/3515bbee37d371e7d999331e79006c5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill out your copyright notice in the Description page of Project Settings. | |
#include "Grabber.h" | |
#include "GameFramework/Actor.h" | |
#include "Engine/World.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; | |
} | |
// Called when the game starts | |
void UGrabber::BeginPlay() | |
{ | |
Super::BeginPlay(); | |
FindPhysicsHandlerComponent(); | |
///Look for attached Physics Handl(only appears at runtime) | |
InputComponent = GetOwner()->FindComponentByClass<UInputComponent>(); | |
if (InputComponent) | |
{ | |
UE_LOG(LogTemp, Warning, TEXT("Input Component found")); | |
///Bind the input axis | |
InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab); | |
InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release); | |
} | |
else | |
{ | |
UE_LOG(LogTemp, Error, TEXT("%s missing input component"), *GetOwner()->GetName()); | |
} | |
} | |
void UGrabber::Grab() { | |
UE_LOG(LogTemp, Warning, TEXT("Grab pressed")); | |
} | |
void UGrabber::Release() { | |
UE_LOG(LogTemp, Warning, TEXT("Grab released")); | |
} | |
void UGrabber::FindPhysicsHandlerComponent() | |
{ | |
//Look for attached Physics Handl | |
PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsComponent>(); | |
if (PhysicsHandle) | |
{ | |
} | |
else | |
{ | |
UE_LOG(LogTemp, Error, TEXT("%s missing handle component"), *GetOwner()->GetName()); | |
} | |
} | |
// 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 | |
//UE_LOG(LogTemp, Warning, TEXT("Player location: %s ,Player rotation: %s"), | |
// *PlayerViewPointLocation.ToString(), | |
// *PlayerViewPointRotation.ToString() | |
//) | |
FVector LineTraceEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach; | |
//Draw a red trace in the world to visual | |
DrawDebugLine( | |
GetWorld(), | |
PlayerViewPointLocation, | |
LineTraceEnd, | |
FColor(255, 0, 0), | |
false, | |
0.f, | |
0.f, | |
10.f | |
); | |
///Setup query params | |
FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner()); | |
///Line-trace (AKA Ray-cast) out to reach distance | |
FHitResult Hit; | |
GetWorld()->LineTraceSingleByObjectType( | |
OUT Hit, | |
PlayerViewPointLocation, | |
LineTraceEnd, | |
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody), | |
TraceParameters | |
); | |
//See what we hit | |
AActor*ActorHit = Hit.GetActor(); | |
if (ActorHit) | |
{ | |
UE_LOG(LogTemp, Warning, TEXT("Line trace hit: %s"), *(ActorHit->GetName())); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill out your copyright notice in the Description page of Project Settings. | |
#pragma once | |
#include "CoreMinimal.h" | |
#include "DrawDebugHelpers.h" | |
#include "Components/ActorComponent.h" | |
#include "PhysicsEngine/PhysicsHandleComponent.h" | |
#include "Components/InputComponent.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: | |
//how far ahead of the player can we reach in cm | |
float Reach = 100.f; | |
UPhysicsComponent* PhysicsHandle = nullptr; | |
UInputComponent* InputComponent = nullptr; | |
//Ray-cast and grab what is in reach | |
void Grab(); | |
void Release(); | |
//find attached physics handle | |
void FindPhysicsHandlerComponent(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment