AI回忆录

该文章展示了如何在UnrealEngine4中编写一个基本的射击游戏项目。它涉及到了Actor类的子类AMyProjectile,使用SphereComponent作为碰撞检测,定义了ProjectileSpeed和OnHit事件处理函数,以及设置了ProjectileMovementComponent来控制弹丸的运动。
摘要由CSDN通过智能技术生成
//use c++ write a first personal shoot game display on ureal4
// 头文件
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/SphereComponent.h"
#include "MyProjectile.generated.h"

UCLASS(config=Game)
class AMyProjectile : public AActor
{
    GENERATED_BODY()

public:
    // 构造函数
    AMyProjectile();

protected:
    // 组件
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Projectile)
    USphereComponent* CollisionComponent;

    // 弹道速度
    UPROPERTY(EditDefaultsOnly, Category = Projectile)
    float ProjectileSpeed;

    // 击中事件
    UFUNCTION()
    void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit);

    // 更新状态
    virtual void Tick(float DeltaTime) override;

    // 开始时
    virtual void BeginPlay() override;
};

// CPP文件
#include "MyProjectile.h"
#include "GameFramework/ProjectileMovementComponent.h"

// 构造函数
AMyProjectile::AMyProjectile()
{
    // 启用物理模拟
    CollisionComponent = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
    CollisionComponent->InitSphereRadius(15.0f);
    CollisionComponent->BodyInstance.SetCollisionProfileName("Projectile");
    CollisionComponent->OnComponentHit.AddDynamic(this, &AMyProjectile::OnHit);
    RootComponent = CollisionComponent;

    // 设置弹道速度
    ProjectileSpeed = 3000.f;

    // 创建弹道移动组件
    UProjectileMovementComponent* ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComponent"));
    ProjectileMovementComponent->SetUpdatedComponent(CollisionComponent);
    ProjectileMovementComponent->InitialSpeed = ProjectileSpeed;
    ProjectileMovementComponent->MaxSpeed = ProjectileSpeed;
    ProjectileMovementComponent->bRotationFollowsVelocity = true;
    ProjectileMovementComponent->bShouldBounce = true;
    ProjectileMovementComponent->Bounciness = 0.3f;

    // 设置初始状态
    InitialLifeSpan = 3.0f;
}

// 击中事件
void AMyProjectile::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
    // 在此处理击中事件
    Destroy();
}

// 更新状态
void AMyProjectile::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
}

// 开始时
void AMyProjectile::BeginPlay()
{
    Super::BeginPlay();
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雾影隐于世

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值