How do I make an FHitResult using a FVector?
- User
How do I make an FHitResult using a FVector?
- Assistant
The
FHitResultandFVectorare two different types of objects in Unreal Engine.FHitResultcontains information about a collision or trace (e.g., line trace or sweep test) that occurred within the scene. It holds data such as the hit location, hit actor, hit component, normal, and other information. In contrast,FVectoris a simple vector class representing 3D coordinates.You can't directly create an
FHitResultusing anFVector, because they represent different kinds of data. However, you can perform a trace or collision test using anFVectoras the starting or ending point and then obtain anFHitResultfrom that operation.Here's a simple example of how you can perform a line trace using two
FVectors and obtain anFHitResult:// Include Headers #include "GameFramework/Actor.h" #include "Engine/World.h" #include "DrawDebugHelpers.h" #include "Kismet/GameplayStatics.h" // In your class void PerformLineTrace() { FVector StartLocation(0.0f, 0.0f, 0.0f); // Replace with your start location FVector EndLocation(100.0f, 0.0f, 0.0f); // Replace with your end location FHitResult HitResult; FCollisionQueryParams TraceParams; // Perform the trace bool bHitSomething = GetWorld()->LineTraceSingleByChannel( HitResult, StartLocation, EndLocation, ECC_Visibility, TraceParams ); // Check if the trace hit something and print the result if (bHitSomething) { UE_LOG(LogTemp, Log, TEXT("Hit: %s"), *HitResult.GetActor()->GetName()); } else { UE_LOG(LogTemp, Log, TEXT("No Hit")); } // Visualize the line trace (optional) DrawDebugLine(GetWorld(), StartLocation, EndLocation, FColor::Red, false, 1.0f, 0, 1.0f); }In this example, we perform a line trace using
StartLocationandEndLocation, which are bothFVectorobjects. If the trace hits an object, we store the information in anFHitResult. Finally, we log the hit actor's name if the trace was successful. - User
I am using UAIBlueprintHelperLibrary::SimpleMoveToLocation(this, DestLocation) to move my character to a location. I am using the mouse, ground location but my character is not moving for some reason.
- Assistant
It seems like you are trying to move your character based on mouse click events. To diagnose why your character is not moving, let's iterate through the steps:
- Ensure that you have an AI Controller set for your character.
Make sure that your character's blueprint or C++ class has an AI Controller assigned. This can be done in the Defaults tab in a Blueprint or via C++.
In C++, you can set the AI Controller class in your character's constructor like this:
// YourCharacterClass.cpp - Constructor AIControllerClass = AYourAIControllerClass::StaticClass();- Detect mouse clicks and get the ground location.
First, ensure you are properly detecting mouse clicks and getting the correct ground location in the form of an
FVector. Check if you are using anonclickedevent or handling left mouse button inputs.- Make sure you're calling SimpleMoveToLocation() correctly.
Check the following things in the code or blueprint where you're calling
UAIBlueprintHelperLibrary::SimpleMoveToLocation():- Make sure you're using the correct AI Controller (
thisshould be the AI Controller and not the character or pawn). - Make sure
DestLocationhas the correct ground location value obtained from your mouse click.
Here's a simple example of how to get a ground location from a mouse click and perform the character movement using SimpleMoveToLocation in a custom Player Controller class:
// Header file #include "CoreMinimal.h" #include "GameFramework/PlayerController.h" #include "MyPlayerController.generated.h" UCLASS() class YOURGAME_API AMyPlayerController : public APlayerController { GENERATED_BODY() public: virtual void SetupInputComponent() override; UFUNCTION() void LeftMouseClick(); }; // CPP file #include "Kismet/GameplayStatics.h" #include "NavigationSystem.h" #include "NavigationPath.h" #include "GameFramework/Character.h" #include "AIController.h" #include "Blueprint/AIBlueprintHelperLibrary.h" void AMyPlayerController::SetupInputComponent() { Super::SetupInputComponent(); InputComponent->BindAction("LeftMouseClick", IE_Pressed, this, &AMyPlayerController::LeftMouseClick); } void AMyPlayerController::LeftMouseClick() { FHitResult HitResult; FVector2D ScreenPosition; this->GetMousePosition(ScreenPosition.X, ScreenPosition.Y); this->DeprojectScreenPositionToWorld(ScreenPosition.X, ScreenPosition.Y, FVector{}, HitResult.Location); ACharacter* ControlledCharacter = GetCharacter(); if (ControlledCharacter) { AAIController* AIController = Cast<AAIController>(ControlledCharacter->GetController()); if (AIController) { UAIBlueprintHelperLibrary::SimpleMoveToLocation(AIController, HitResult.Location); } } }Lastly, you could add log statements or breakpoints to see if the code is being executed when you click on the ground, and if the character's movement request is being initiated properly. This will help you identify if any specific part of the code is causing the issue.