虚幻四C++入坑指南13:使用C++给子弹添加爆炸效果

虚幻四C++入坑指南13:使用C++给子弹添加爆炸效果

00 项目设置:FPS C++ 带初学者内容

01 FPS_Cpp_WtProjectile(一般加了中文注释才是新添加的代码,其它都是文件自带的)

FPS,第一人称设计
Cpp,带C++类
Wt,With Starter Cntent,带初学者内容
在这里插入图片描述

01 01 FPS_Cpp_WtProjectile.h

// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Particles/ParticleSystem.h"
#include "FPS_Cpp_WtProjectile.generated.h"

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

	/** Sphere collision component */
	UPROPERTY(VisibleDefaultsOnly, Category=Projectile)
	class USphereComponent* CollisionComp;

	/** Projectile movement component */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = "true"))
	class UProjectileMovementComponent* ProjectileMovement;

public:
	AFPS_Cpp_WtProjectile();

	/** called when projectile hits something */
	UFUNCTION()
	void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);

	/** Returns CollisionComp subobject **/
	FORCEINLINE class USphereComponent* GetCollisionComp() const { return CollisionComp; }
	/** Returns ProjectileMovement subobject **/
	FORCEINLINE class UProjectileMovementComponent* GetProjectileMovement() const { return ProjectileMovement; }
//不赋死值
protected:
	//EditDefaultOnly少了s
	UPROPERTY(EditDefaultsOnly,Category="Emitter")
		//#include "Particles/ParticleSystem.h"
		UParticleSystem* Emitter_Projectile;
};

01 02 FPS_Cpp_WtProjectile.cpp

// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.

#include "FPS_Cpp_WtProjectile.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Components/SphereComponent.h"
#include "Kismet/GameplayStatics.h"

AFPS_Cpp_WtProjectile::AFPS_Cpp_WtProjectile() 
{
	// Use a sphere as a simple collision representation
	CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComp"));
	CollisionComp->InitSphereRadius(5.0f);
	CollisionComp->BodyInstance.SetCollisionProfileName("Projectile");
	CollisionComp->OnComponentHit.AddDynamic(this, &AFPS_Cpp_WtProjectile::OnHit);		// set up a notification for when this component hits something blocking

	// Players can't walk on it
	CollisionComp->SetWalkableSlopeOverride(FWalkableSlopeOverride(WalkableSlope_Unwalkable, 0.f));
	CollisionComp->CanCharacterStepUpOn = ECB_No;

	// Set as root component
	RootComponent = CollisionComp;

	// Use a ProjectileMovementComponent to govern this projectile's movement
	ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileComp"));
	ProjectileMovement->UpdatedComponent = CollisionComp;
	ProjectileMovement->InitialSpeed = 3000.f;
	ProjectileMovement->MaxSpeed = 3000.f;
	ProjectileMovement->bRotationFollowsVelocity = true;
	ProjectileMovement->bShouldBounce = true;

	// Die after 3 seconds by default
	InitialLifeSpan = 3.0f;
}
//************************************************************
void AFPS_Cpp_WtProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
	// Only add impulse and destroy projectile if we hit a physics
	if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL) && OtherComp->IsSimulatingPhysics())
	{
		OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());

		Destroy();
	}
	/*
	UParticleSystemComponent* UGameplayStatics::SpawnEmitterAtLocation(
		const UObject* WorldContextObject, 关卡地址
		UParticleSystem* EmitterTemplate, 
		FVector SpawnLocation, 
		FRotator SpawnRotation, 
		bool bAutoDestroy, 
		EPSCPoolMethod PoolingMethod, 
		bool bAutoActivateSystem)
	{
		return SpawnEmitterAtLocation(
			WorldContextObject, 
			EmitterTemplate, 
			SpawnLocation, 
			SpawnRotation, 
			FVector(1.f), 
			bAutoDestroy, 
			PoolingMethod, 
			bAutoActivateSystem
		);
	}
	*/
	//特定位置加特效,特效名称,特效位置,自动销毁(默认true)
	/** Getter for the cached world pointer, will return null if the actor is not actually spawned in a level */
	//virtual UWorld* GetWorld() const override;//GameFramework/Actor.h
	//UGameplayStatics需要Kismet/GameplayStatics.h
	//三个重载
	//也行,UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), Emitter_Projectile, GetActorLocation());
	UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), Emitter_Projectile, this->GetTransform());
}

02 子弹蓝图

02 打开

在这里插入图片描述

02 02 添加特效

在这里插入图片描述

03 演示

在这里插入图片描述

使用的 主要技术有: 1、大面积山脉渲染,使用Heightmap构造地形,7级 LOD 细节精度,地形无限重复循环 。 2、清晰、自然的地表贴图。 3、读取.MD2 、.MS3D 格式的3D模型文件。 4、粒子系统,产生爆炸、炊烟等特效。 5、太阳光晕。 6、使用Blend模拟Brightness/contrast调节图象亮度。 操作控制 可以在GameSetting菜单中设定: 1.视频属性 ( Video Setting ) 1).屏幕分辨率( Resolution ) 游戏率默认为800*600,在任务执行前可改变分辨率,但不会立即生效,只有在初始化任务时才改变分辨率。 2).屏幕亮度( Brigthness ) 在不同的硬件配置上,屏幕亮度往往表现出较大差异,通过该项可将亮度调节到最佳。 3).视野范围 ( Visible Distance ) 调节地形绘制的距离。对于配置较低的硬件,适当降低视野距离可提高帧速率,但由于远处地形网格较粗,对帧速率提高贡献并不大。 4).雾浓度 ( Fog Density ) 2.音频属性 ( Audio Setting ) 1).背景音乐(music) 可以打开或关闭背景音乐,可以调节音量。 音乐播放 audio/music/menu.mp3 ,如果你有自己喜爱的mp3音乐文件可以将它替换 menu.mp3 文件。 2).音效( sound ) 可以打开或关闭音效,可以调节音量。游戏中的枪声与人物的发声具有3D效果。 3.键盘操作 ( Keyboard Setting ) 以下操作可以更改 Up、Down、Left、Right设定移动操作。 Fire 射击 Jump 跳跃 Zoom 放大远处景物 Help 弹出帮助 修改方法:用鼠标点击选项,然后输入新的按键。 另外,游戏保留了几个开发模式下的操作: Page Up 提升视点高度 Page Down 降低视点高度 (可以看到地形绘制区域) L 线框模式 F 冻结所有敌人 V 隐身 N 敌人攻击力为零 I 隐藏房子 O 隐藏敌人 P 隐藏树木 在游戏运行中,按 Help 项的按键获得帮助。 4.鼠标操作 鼠标的移动可改变视角,默认设定左键为射击,右键为放大。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值