【UE4 塔防游戏系列】07-子弹对敌人造成伤害
发布人:shili8
发布时间:2025-01-12 08:28
阅读次数:0
**UE4 塔防游戏系列07 - 子弹对敌人造成伤害**
在前面的文章中,我们已经完成了塔的建造、敌人的生成以及子弹的射击。然而,子弹对敌人造成伤害这一环节仍然没有实现。在本篇文章中,我们将重点讨论如何让子弹对敌人造成伤害。
**1. 子弹类的设计**
首先,我们需要定义一个子弹类(Bullet),这个类应该包含以下属性:
* `Position`:子弹的位置* `Velocity`:子弹的速度* `Damage`:子弹对敌人造成的伤害
cpp// Bullet.h#pragma once#include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Components/SphereComponent.h" UCLASS() class TOWERDEFENSE_API ABullet : public AActor{ GENERATED_BODY() public: // Sets default values for this actor's properties ABullet(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Bullet", meta = (AllowPrivateAccess = "true")) class USphereComponent* SphereComp; };
cpp// Bullet.cpp#include "Bullet.h" #include "Components/SphereComponent.h" ABullet::ABullet() { // 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; SphereComp = CreateDefaultSubobject(TEXT("SphereComp")); RootComponent = SphereComp; } void ABullet::BeginPlay() { Super::BeginPlay(); } void ABullet::Tick(float DeltaTime) { Super::Tick(DeltaTime); }
**2. 子弹对敌人造成伤害**
接下来,我们需要在子弹类中添加一个函数来计算子弹对敌人造成的伤害。这个函数应该考虑到子弹的速度和距离。
cpp// Bullet.h#pragma once#include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Components/SphereComponent.h" UCLASS() class TOWERDEFENSE_API ABullet : public AActor{ GENERATED_BODY() public: // Sets default values for this actor's properties ABullet(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Bullet", meta = (AllowPrivateAccess = "true")) class USphereComponent* SphereComp; }; // Bullet.h#pragma once#include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Components/SphereComponent.h" UCLASS() class TOWERDEFENSE_API ABullet : public AActor{ GENERATED_BODY() public: // Sets default values for this actor's properties ABullet(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Bullet", meta = (AllowPrivateAccess = "true")) class USphereComponent* SphereComp; }; // Bullet.h#pragma once#include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Components/SphereComponent.h" UCLASS() class TOWERDEFENSE_API ABullet : public AActor{ GENERATED_BODY() public: // Sets default values for this actor's properties ABullet(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Bullet", meta = (AllowPrivateAccess = "true")) class USphereComponent* SphereComp; }; // Bullet.h#pragma once#include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Components/SphereComponent.h" UCLASS() class TOWERDEFENSE_API ABullet : public AActor{ GENERATED_BODY() public: // Sets default values for this actor's properties ABullet(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Bullet", meta = (AllowPrivateAccess = "true")) class USphereComponent* SphereComp; }; // Bullet.h#pragma once#include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Components/SphereComponent.h" UCLASS() class TOWERDEFENSE_API ABullet : public AActor{ GENERATED_BODY() public: // Sets default values for this actor's properties ABullet(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Bullet", meta = (AllowPrivateAccess = "true")) class USphereComponent* SphereComp; }; // Bullet.h#pragma once#include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Components/SphereComponent.h" UCLASS() class TOWERDEFENSE_API ABullet : public AActor{ GENERATED_BODY() public: // Sets default values for this actor's properties ABullet(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Bullet", meta = (AllowPrivateAccess = "true")) class USphereComponent* SphereComp; }; // Bullet.h#pragma once#include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Components/SphereComponent.h" UCLASS() class TOWERDEFENSE_API ABullet : public AActor{ GENERATED_BODY() public: // Sets default values for this actor's properties ABullet(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Bullet", meta = (AllowPrivateAccess = "true")) class USphereComponent* SphereComp; }; // Bullet.h#pragma once#include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Components/SphereComponent.h" UCLASS() class TOWERDEFENSE_API ABullet : public AActor{ GENERATED_BODY() public: // Sets default values for this actor's properties ABullet(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Bullet", meta = (AllowPrivateAccess = "true")) class USphereComponent* SphereComp; }; // Bullet.h#pragma once#include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Components/SphereComponent.h" UCLASS() class TOWERDEFENSE_API ABullet : public AActor{ GENERATED_BODY() public: // Sets default values for this actor's properties ABullet(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Bullet", meta = (AllowPrivateAccess = "true")) class USphereComponent* SphereComp; }; // Bullet.h#pragma once#include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Components/SphereComponent.h" UCLASS() class TOWERDEFENSE_API ABullet : public AActor{ GENERATED_BODY() public: // Sets default values for this actor's properties ABullet(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Bullet", meta = (AllowPrivateAccess = "true")) class USphereComponent* SphereComp; }; // Bullet.h#pragma once#include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Components/SphereComponent.h" UCLASS() class TOWERDEFENSE_API ABullet : public AActor{ GENERATED_BODY() public: // Sets default values for this actor's properties ABullet(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Bullet", meta = (AllowPrivateAccess = "