11715人加入学习
(26人评价)
【旧版】Unreal基本知识案例 - 密室逃脱

旧版课程,制作完成于2017-01-11

价格 免费

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 可能为空,需要校验
}

 

[展开全文]

授课教师

SiKi学院老师

课程特色

图文(2)
视频(37)