if(Actor != nullptr)
和
if(Actor)
效果一样
指针、对象 也好,都可以这样判断不为空执行if语句
#include "Grabber.h"
#include "Engine/World.h"
#include "DrawDebugHelpers.h"
UGrabber::UGrabber()
{
PrimaryComponentTick.bCanEverTick = true;
}
void UGrabber::BeginPlay()
{
Super::BeginPlay();
}
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}
FVector UGrabber::GetLineStart()
{
FVector Start;
FRotator ViewRotator;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(Start, ViewRotator);
return Start;
}
FVector UGrabber::GetLineEnd()
{
FVector Start;
FRotator ViewRotator;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(Start, ViewRotator);
return (Start + ViewRotator.Vector() * 100); // ViewRotator.Vector() 视野的正中心 前方 100cm
}
AActor * UGrabber::LineTrace()
{
//DrawDebugLine(GetWorld(), Start, End, FColor(255, 0, 0), false, 0, 0, 10);
FHitResult Hit;
GetWorld()->LineTraceSingleByObjectType(
Hit,
GetLineStart(),
GetLineEnd(),
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
FCollisionQueryParams(FName(TEXT("")), false, GetOwner())
);
AActor* Actor = Hit.GetActor();
if (Actor != nullptr)
{
UE_LOG(LogTemp, Warning, TEXT("Line Hit:%s"), *Actor->GetName());
}
return Actor;//Actor 可能为空,需要校验
}