Hello! How do I implement picking of objects (for example flashlight) with Blueprints?
I tried to search for information on YouTube, but found nothing. Help me, please.
what do you mean by picking? do you mean like have your player pick it up? if thats the case what do you want to happen then? add to a inventory? place in hand?
give more info to make your intent clear.
Here you go:
void MyPawn::PickObjectOnMouseClick(class ASLPlayerController* playerController,const float mouseX,const float mouseY)
{
FVector2D MousePosition(mouseX, mouseY);
FHitResult HitResult;
const bool bTraceComplex = false;
if (playerController->GetHitResultAtScreenPosition(MousePosition, ECC_Visibility, bTraceComplex, HitResult) == true)
{
// If the actor we intersected with is a controller posses it
AActor* hitACtor = HitResult.GetActor();
UE_LOG(LogTemp, Warning, TEXT("Hit actor name is:%s\n"), *hitACtor->GetName());
//do smth with picked actor here
}
//}
}
Couple of comments regarding the code:
ASLPlayerController is my own extension of PlayerController.But if you use default one you can get its pointer by calling GetController() then casting it to PlayerController …To get mouse X / Y make sure you bind mouse X and mouse Y axis. (See documentation online,it is easy).
This method doesn’t use pure raycasting approach. Unreal API has a function GetHitResultAtScreenPosition which wraps all the complexity of casting ray and converting mouse screen coordinates to world’s.So as long as the goal is to pick stuff in 3d scene based on mouse input,this is the way to go.
Sorry, I speak English not well, because I’m school boy from Russia. Yeah, I mean that player can pick it up. I need an icon to appear on the screen like this:
And for example, if I didn’t have this item, I couldn’t use it. Like a flashlight here:
Thanks in advance.