射线检测:
FHitResult Hit;
GetWorld()->LineTraceSingleByObjectType
(
Hit,
Start,
End,
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
FCollisionQueryParam(FName(TEXT("")), false, GetOwner())
);
射线检测:
FHitResult Hit;
GetWorld()->LineTraceSingleByObjectType
(
Hit,
Start,
End,
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
FCollisionQueryParam(FName(TEXT("")), false, GetOwner())
);
射线检测:有起点有方向有距离
ECC_PhysicsBody 物理物体
细节面板上的 Physics ,勾选 Simulate Physics 才会触发碰撞,
射线触发一定要勾选上 细节面板上的Physics下的 Simulate Physics 才会触发
Grabber.cpp
#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);
FHitResult Hit;
GetWorld()->LineTraceSingleByObjectType(
Hit,
Start,
End,
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());
}
}
FVector Start;
FRotator ViewRotator;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(Start, ViewRotator);
FVector End = Start + ViewRotator.Vector()*100;
DrawDebugLine(GetWorld(), Start, End, FColor(255, 0, 0), false, 0, 0, 10);
当Simulate Physics勾选完后,物体受模拟物理效果影响,如重力。
Simulation Generates Hit Events 是"当此对象在物理模拟中发生碰撞时是否发送命中事件"
GetPlayerViewPoint()