还是使用上次创建的浮动平台
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FloatingActor.generated.h"
UCLASS()
class AFloatingActor : public AActor {
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFloatingActor();
UPROPERTY(VisibleAnywhere, Category = "AAA")
UStaticMeshComponent* VisualMesh;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
//子弹爆炸的粒子效果
UPROPERTY(EditDefaultsOnly, Category = "AAA")
class UParticleSystem* projectileParticle;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
//线程函数
UFUNCTION()
void checkOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult&SweepResult);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "FloatingActor.h"
#include "Components/SceneComponent.h"
#include "TrueFPSProjectProjectile.h"
#include "Particles/ParticleSystem.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
AFloatingActor::AFloatingActor() {
// 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;
//if (USceneComponent * ExistingRootComponent = GetRootComponent()) {
VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
VisualMesh->SetupAttachment(RootComponent);
}
// Called when the game starts or when spawned
void AFloatingActor::BeginPlay() {
Super::BeginPlay();
VisualMesh->OnComponentBeginOverlap.AddDynamic(this, &AFloatingActor::checkOverlap);
}
// Called every frame
void AFloatingActor::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
FVector NewLocation = GetActorLocation();
FRotator NewRotation = GetActorRotation();
//获取物体当前的运行时间
float RunningTime = GetGameTimeSinceCreation();
float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
NewLocation.Z += DeltaHeight * 20.0f; //Scale our height by a factor of 20
float DeltaRotation = DeltaTime * 20.0f; //Rotate by 20 degrees per second
NewRotation.Yaw += DeltaRotation;
SetActorLocationAndRotation(NewLocation, NewRotation);
}
void AFloatingActor::checkOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
ATrueFPSProjectProjectile* projectile = Cast<ATrueFPSProjectProjectile>(OtherActor);
if (projectile){
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), projectileParticle, GetTransform());
projectile->Destroy();
Destroy();
}
}
新建一个包含AFloatingActor的Actor,命名为:SpwanActor
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SpawnActor.generated.h"
UCLASS()
class TRUEFPSPROJECT_API ASpawnActor : public AActor {
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ASpawnActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
public:
UPROPERTY(EditAnywhere, Category = "AAA")
TSubclassOf<class AFloatingActor> subFloatingActor;
float baseTime = 0.0;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "SpawnActor.h"
#include "FloatingActor.h"
// Sets default values
ASpawnActor::ASpawnActor() {
// 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;
}
// Called when the game starts or when spawned
void ASpawnActor::BeginPlay() {
Super::BeginPlay();
}
// Called every frame
void ASpawnActor::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
//每隔3秒生产一个Actor
if ((baseTime += DeltaTime) <= 3.0){
return;
}
baseTime = 0.0;
UWorld* world = GetWorld();
if (world){
FVector spawnLocation = GetActorLocation();
FRotator spwanRotation = GetActorRotation();
FActorSpawnParameters spawnParam;
spawnParam.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;
world->SpawnActor<AFloatingActor>(subFloatingActor, spawnLocation, spwanRotation, spawnParam);
}
}
aaa