添加抓取功能:
在DefaultPawn_Blueprint文件中:
添加组件---新建C++组件...
第二步,选择Actor Component :
第三步,名称改称为Grabber,抓取器:
创建类
Unreal Engine 4 中的坐标/向量 FVector
FVector Start;
获取PlayerController()
#include "Engine/World.h"
GetWorld()->GetFirstPlayerController()
PlayerController : 控制Player的移动的
使用DrawDebugLine
需要引用 #include "DrawDebugHelpers.h"
DrawDebugLine(GetWorld(), Start, End, FColor(255, 0, 0), false, 0, 0, 10);
API
void DrawDebugLine()
{
const UWorld * InWorld,
FVector const & LineStart,
FVector const & LineEnd,
FColor const & Color,
bool bPersistentLines;
float LifeTime,
uint8 DepthPriority,
float Thickness
}
编译没看到线,先保存一下蓝图类DefaultPawn_Blueprint !!!
#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 Start;
FRotator ViewRotator;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(Start, ViewRotator);
FVector End = Start + ViewRotator.Vector()*100; // ViewRotator.Vector() 视野的正中心 前方 100cm
DrawDebugLine(GetWorld(), Start, End, FColor(255, 0, 0), false, 0, 0, 10);
}