ue4抛射物击中物体的销毁
物体要产生碰撞的要求:
1.两个物体必须产生overlap
2.勾选产生重叠事件
子弹Projectile

子弹对象类型是Projectile
FloatingAcotor


红色代码为销毁代码
floatactor.h中
// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "floatActor.generated.h" UCLASS() class NEWFPS_API AfloatActor : public AActor { GENERATED_BODY() public: // Sets default values for this actor's properties AfloatActor(); UPROPERTY(visibleAnywhere,category="Static") UStaticMeshComponent *FloatingMesh; UFUNCTION() void OnCollision(class UPrimitiveComponent* HitComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; };
floatactor.cpp中
// Fill out your copyright notice in the Description page of Project Settings. #include "floatActor.h" #include "NewFPSProjectile.h" // Sets default values AfloatActor::AfloatActor() { // 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; FloatingMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("FloatingMesh")); FloatingMesh->SetupAttachment(RootComponent); static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube'")); if (CubeVisualAsset.Succeeded()) //如果找到了 { FloatingMesh->SetStaticMesh(CubeVisualAsset.Object); FloatingMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f)); } } // Called when the game starts or when spawned void AfloatActor::BeginPlay() { Super::BeginPlay(); FloatingMesh->OnComponentBeginOverlap.AddDynamic(this, &AfloatActor::OnCollision); } // Called every frame void AfloatActor::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; float DeltaRotation = DeltaTime * 20.0f; //每秒旋转20度 NewRotation.Yaw += DeltaRotation; //Yaw:摇头 pitch:点头 roll旋转 SetActorLocationAndRotation(NewLocation, NewRotation); } //OtherActor谁对我发生了碰撞 void AfloatActor::OnCollision(class UPrimitiveComponent* HitComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) { //将OtherActor转换成ANewFPSProjectile类型代表发生了碰撞 //和FloatingActor发生碰撞的物体能够转换成子弹类型的话代表FloatingActor和子弹进行碰撞了 ANewFPSProjectile* Projectile = Cast<ANewFPSProjectile>(OtherActor); if (Projectile == nullptr) //代表OtherActor物体没有被转换成子弹类型所以不需要管 { return; } else { this->Destroy(); } }

浙公网安备 33010602011771号