新手学习UE4C++编程,在Udemy上学习的教程,第一个挑战任务,制作一个黑洞,以下为解答:
照着做的,不是所有代码都能看明白,请多指教
- 创建一个Actor
- 为Actor创建根组件MeshComp
- 创建外圆为Actor提供Overlap事件判断吸入范围
- 创建内圆为Actor提供Overlap事件判断是否销毁吸入的对象
- 为内圆创建重叠事件
FPSBlackHole.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FPSBlackHole.generated.h"
class UStaticMeshComponent;
class USphereComponent;
UCLASS()
class FPSGAME_API AFPSBlackHole : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFPSBlackHole();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
//创建网格体
UPROPERTY(VisibleAnywhere, Category = "Components")
UStaticMeshComponent* MeshComp;
//创建内圆
UPROPERTY(VisibleAnywhere, Category = "Components")
USphereComponent* InnerSphereComponent;
//创建外圆
UPROPERTY(VisibleAnywhere, Category = "Components")
USphereComponent* OuterSphereComponent;
//重叠事件
UFUNCTION()
void OverlapInnerSphere(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
FPSBlackHole.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "FPSBlackHole.h"
#include "Components/SphereComponent.h"
#include "Components/StaticMeshComponent.h"
// Sets default values
AFPSBlackHole::AFPSBlackHole()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));//创建
MeshComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);//去除碰撞
RootComponent = MeshComp;//设为根组件
InnerSphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("InnerSphereComponent"));//创建
InnerSphereComponent->SetSphereRadius(100);//设置半径100
InnerSphereComponent->SetupAttachment(MeshComp);//绑定到Meshcomp
InnerSphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AFPSBlackHole::OverlapInnerSphere);
OuterSphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("OuterSphereComp"));//创建外圆
OuterSphereComponent->SetSphereRadius(3000);//设置半径
OuterSphereComponent->SetupAttachment(MeshComp);//附加到MeshComp
}
// Called when the game starts or when spawned
void AFPSBlackHole::BeginPlay()
{
Super::BeginPlay();
}
void AFPSBlackHole::OverlapInnerSphere(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor)
{
OtherActor->Destroy();
}
}
// Called every frame
void AFPSBlackHole::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
TArray<UPrimitiveComponent*>OverlappingComps;
OuterSphereComponent->GetOverlappingComponents(OverlappingComps);
for (int32 i=0;i<OverlappingComps.Num();i++)
{
UPrimitiveComponent* PrimComp = OverlappingComps[i];
if (PrimComp&&PrimComp->IsSimulatingPhysics())
{
const float SphereRadius = OuterSphereComponent->GetScaledSphereRadius();
const float ForceStrength = -2000;
PrimComp->AddRadialForce(GetActorLocation(), SphereRadius, ForceStrength, ERadialImpulseFalloff::RIF_Constant, true);
}
}
}