Unreal Engine 4 C++ 射線碰撞
來源:程序員人生 發(fā)布時間:2014-10-08 08:00:01 閱讀次數(shù):4451次
游戲開發(fā)中經(jīng)常會用到射線碰撞,比如激光器打一槍,需要明確知道它集中的位置,然后在這個點釋放攻擊特效。
Unrea Engine 4中做射線碰撞也很簡單,主要功能的實現(xiàn)是World的LineTraceSingle這個方法,下面給出測試代碼。代碼我在ThirdPersonTemplate中測試。檢測角色正前方有無柯碰撞的Actor,有就在碰撞點上顯示一個調(diào)試用的圓球。代碼如下:
void ANanProjectCharacter::Raycast()
{
FHitResult hitResult(ForceInit);
FVector pos, dir;
FCollisionQueryParams ccq(FName(TEXT("CombatTrace")), true, NULL);
ccq.bTraceComplex = true;
ccq.bTraceAsyncScene = false;
ccq.bReturnPhysicalMaterial = false;
ccq.AddIgnoredActor(this);
pos = GetActorLocation();
const FRotator Rotation = CapsuleComponent->GetComponentRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
dir = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
FVector posBegin = pos;
FVector posEnd = pos + dir * 500;
GetWorld()->LineTraceSingle(hitResult,
posBegin,
posEnd,
ECC_WorldStatic,
ccq);
DrawDebugLine(this->GetWorld(), posBegin, posEnd, FColor(1.0f, 0.f, 0.f, 1.f), false, 20.f);
if (hitResult.GetActor())
{
DrawDebugSphere(GetWorld(), hitResult.Location, 10, 10, FColor::Red, false, 20.f);
}
}
注意下,需要把角色自己從碰撞檢測中排除掉。最終效果如下:

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機掃描二維碼進行捐贈