Skip to content

Commit

Permalink
[Add] Raycasting
Browse files Browse the repository at this point in the history
  • Loading branch information
iJotape4 committed Oct 20, 2024
1 parent aceaa0c commit 8a7e144
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 7 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions GameJamPlus2024/Config/DefaultEditor.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ bReplaceBlueprintWithClass= true
bDontLoadBlueprintOutsideEditor= true
bBlueprintIsNotBlueprintType= true

[/Script/AdvancedPreviewScene.SharedProfiles]

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class GameJamPlus2024 : ModuleRules
{
public GameJamPlus2024(ReadOnlyTargetRules Target) : base(Target)
{
PrivateDependencyModuleNames.AddRange(new string[] { "AITestSuite", "AITestSuite", "AITestSuite" });
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput", "Paper2D", "Paper2D", "UMGEditor", "UMGEditor" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "EnhancedInputSubsystems.h"
#include "EventsManager.h"
#include "PaperFlipbookComponent.h"
#include "Camera/CameraComponent.h"
#include "GameJamPlus2024/GameJamPlus2024Character.h"
#include "Hazards/HazardBase.h"

Expand Down Expand Up @@ -42,7 +43,11 @@ void APaperCharacterBase::SetupPlayerInputComponent(class UInputComponent* Playe
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);

// Grappling Hook
EnhancedInputComponent->BindAction(GrapplingHookAction, ETriggerEvent::Triggered, GrapplingHookComponent, &UGrapplingHookComponent::LaunchHook);

//Bind Action, and call the hook launch method in the GrapplingHookComponent passing the mouse position
EnhancedInputComponent->BindAction(GrapplingHookAction, ETriggerEvent::Triggered, GrapplingHookComponent, &UGrapplingHookComponent::LaunchHook);

//Cast<APlayerController>(GetController())->GetMousePosition(MousePosition.X, MousePosition.Y);
}
else
{
Expand Down Expand Up @@ -127,10 +132,44 @@ void APaperCharacterBase::TakeDamage(int Damage)
}


void APaperCharacterBase::EnableMouseEvents()
{
APlayerController* PC = Cast<APlayerController>(GetController());

if (PC)
{
PC->bShowMouseCursor = true;
PC->bEnableClickEvents = true;
PC->bEnableMouseOverEvents = true;
}
}

FVector2D APaperCharacterBase::GetMousePosition()
{
FVector2D MousePosition(0.0f, 0.0f);
Cast<APlayerController>(GetController())->GetMousePosition(MousePosition.X, MousePosition.Y);
return FVector2D(MousePosition);
}

FVector APaperCharacterBase::ConvertScreenToWorldPoint(const FVector2D& ScreenPosition)
{
FVector WorldLocation;
FVector WorldDirection = GetActorRightVector();
APlayerController* PlayerController = Cast<APlayerController>(GetController());
FVector PlayerLocation = PlayerController->GetPawn()->GetActorLocation();
//UE_LOG(LogTemp, Log, TEXT("Player Location: X = %f, Y = %f, Z = %f"), PlayerLocation.X, PlayerLocation.Y, PlayerLocation.Z);
PlayerController->DeprojectScreenPositionToWorld(ScreenPosition.X, ScreenPosition.Y, WorldLocation,WorldDirection);

//return FVector(PlayerLocation.X, WorldLocation.X, WorldLocation.Y);
return WorldLocation;
}

void APaperCharacterBase::BeginPlay()
{
Super::BeginPlay();
OnActorBeginOverlap.AddDynamic(this, &APaperCharacterBase::OnBeginOverlap);

EnableMouseEvents();
}

void APaperCharacterBase::OnBeginOverlap(AActor* OverlappedActor, AActor* OtherActor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,82 @@

#include "Upgrades/GrapplingHookComponent.h"

void UGrapplingHookComponent::LaunchHook()
#include <optional>

#include "DrawDebugHelpers.h" // For visualizing the trace in the editor (optional)
#include "PaperCharacterBase.h"
#include "Camera/CameraComponent.h"
#include "Engine/World.h" // For accessing the world
#include "GameFramework/Actor.h" // For working with actors
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetSystemLibrary.h" // Optional for advanced tracing


void UGrapplingHookComponent::LaunchHook(const FInputActionValue& Value)
{
UE_LOG(LogTemp, Warning, TEXT("Hook Launched!"));
APaperCharacterBase* Character = Cast<APaperCharacterBase>(GetOwner());

FHitResult HitResult; // The hit result of the line trace
HookLineTrace(HitResult); // Perform the line trace
}

void UGrapplingHookComponent::HookLineTrace(FHitResult& OutHit)
{
// UCameraComponent* Camera = GetOwner()->FindComponentByClass<UCameraComponent>();
// if (!Camera) return;
//
// APaperCharacterBase* Character = Cast<APaperCharacterBase>(GetOwner());
// if (!Character) return;
//
// APlayerController* PlayerController = Cast<APlayerController>(Character->GetController());
// if (!PlayerController) return;
//
// FVector2D MousePosition(0.0f, 0.0f);
// Cast<APlayerController>(PlayerController)->GetMousePosition(MousePosition.X, MousePosition.Y);
//
// FVector WorldLocation;
// FVector WorldDirection;
// float Distance = 2000.0f;
//
// // Deproject the mouse position to get the world direction
// if (PlayerController->DeprojectMousePositionToWorld(WorldLocation, WorldDirection))
// {
// // Set the trace start and end points
// FVector Start = Camera->GetComponentTransform().GetLocation(); // The character's location
// FVector End = WorldLocation + (WorldDirection * Distance) ; // Adjust the distance as needed (10,000 units)
//
// ///////// LINE PLANE INTERSECTION
// FVector PlaneOrigin = Character->GetActorLocation(); // A point on the plane (e.g., origin)
// FVector PlaneNormal(1.0f, 0.0f, 0.0f); // The plane's normal (e.g., x-axis)
// /// // Compute the dot product between the line direction and the plane normal
// float Denominator = FVector::DotProduct(PlaneNormal, End);
//
// // Check if the line is parallel to the plane
// if (FMath::IsNearlyZero(Denominator)) {
// return; // No intersection (or line is parallel to the plane)
// }
//
// // Compute the parameter 't' for the intersection point
// float t = FVector::DotProduct(PlaneNormal, PlaneOrigin - Start) / Denominator;
//
// // Compute the intersection point using the parametric line equation
// FVector IntersectionPoint = Start + t * End;
//
//
// /////////// Perform the line trace
// FHitResult HitResult;
// FCollisionQueryParams CollisionParams;
// CollisionParams.AddIgnoredActor(GetOwner()); // Ignore the character itself
//
// bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, PlaneOrigin, IntersectionPoint, ECC_Visibility, CollisionParams);
//
// if (bHit)
// {
// // Handle hit (e.g., log hit actor's name)
// UE_LOG(LogTemp, Warning, TEXT("Hit: %s"), *HitResult.GetActor()->GetName());
// }
//
// // Optional: Draw debug line for visualization
// DrawDebugLine(GetWorld(), Start, End, FColor::Red, false, 4.0f, 0, 1.0f);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ class GAMEJAMPLUS2024_API APaperCharacterBase : public APaperCharacter

public:
APaperCharacterBase();

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Upgrades", meta = (AllowPrivate))
UDoubleJumpComponent* DoubleJumpComponent;

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Upgrades", meta = (AllowPrivate))
UGrapplingHookComponent* GrapplingHookComponent;


protected:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input", meta = (AllowPrivate = "true"))
float CameraRotationAngle = 90.0f;
Expand All @@ -58,12 +59,18 @@ class GAMEJAMPLUS2024_API APaperCharacterBase : public APaperCharacter
void SwitchAnimation(const FVector2d& Value);
void RotateCamera(const FInputActionValue& Value);
void TakeDamage(int Damage);
void EnableMouseEvents();


// APawn interface
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

// To add mapping context
virtual void BeginPlay() override;
public:
FVector2D GetMousePosition();
FVector ConvertScreenToWorldPoint(const FVector2D& ScreenPosition);

private:

UFUNCTION()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ UCLASS()
class GAMEJAMPLUS2024_API UGrapplingHookComponent : public UCharacterUpgrade
{
GENERATED_BODY()

public:
void LaunchHook();

protected:
void HookLineTrace(FHitResult& OutHit);
public:
void LaunchHook(const FInputActionValue& Value);

};

0 comments on commit 8a7e144

Please sign in to comment.