对魔法弹的设置较为简单,只是在初始化的时候对几个属性进行赋值罢了。跟着斯坦福的教程来,应该不会有什么问题,有问题评论区问我。
魔法弹代码:(楼主丈育,魔法子弹多打了一个T,先不改了,将错就错)
SMagicProjectTile.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SMagicProjectTile.generated.h"
UCLASS()
class ACTIONROGUELIKE_API ASMagicProjectTile : public AActor
{
GENERATED_BODY()
//球形碰撞体组件
UPROPERTY(VisibleAnywhere)
class USphereComponent* SphereComp;
//运动组件,设置初始速度,是否受重力影响啥的
UPROPERTY(VisibleAnywhere)
class UProjectileMovementComponent* MovementComp;
//粒子系统组件,用来展示粒子效果
UPROPERTY(VisibleAnywhere)
class UParticleSystemComponent* EffectComp;
public:
// Sets default values for this actor's properties
ASMagicProjectTile();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
SMagicProjectTile.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "SMagicProjectTile.h"
#include "Components/SphereComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Particles/ParticleSystemComponent.h"
// Sets default values
ASMagicProjectTile::ASMagicProjectTile()
{
// 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<USphereComponent>(TEXT("SephereComp"));
//使用Projectile的通道检测方案(个人理解,具体看斯坦福的那个老师怎么讲)
SphereComp->SetCollisionProfileName("Projectile");
//设置根组件
RootComponent = SphereComp;
//实例化运动组件
MovementComp = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("MoveComp"));
//设置初速度
MovementComp->InitialSpeed = 1000.0f;
//如为true,则发射物将在每帧更新其旋转,让旋转匹配速度方向
//讲人话就是永远面朝速度的正方向
MovementComp->bRotationFollowsVelocity = true;
//如为true,初速度将被解译为启动时便在本地空间中
//我的理解是,如果为false,那么子弹的发射方向只会朝向一个方向,如果为true,那么子弹发射的方向取决于我们摄像机的方向,也就是我们控制器的方向。
MovementComp->bInitialVelocityInLocalSpace = true;
//设置子弹的重力比率,如果为0,那么就是没有重力,如果是1,那么就是正常重力,大于1也可以
MovementComp->ProjectileGravityScale = 0.0f;
//实例化例子系统组件
EffectComp = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("EffectComp"));
EffectComp->SetupAttachment(SphereComp);
}
// Called when the game starts or when spawned
void ASMagicProjectTile::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ASMagicProjectTile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
有了魔法弹,我们人物也得增加一个攻击的功能。按照那个老师的说法,是为了系统的可拓展性,所以这里不会写死,而是写一个模板子类,而我们攻击的时候就生成这个类的对象。而具体的类是我们在编辑器里头去写的。
相应的,我们需要在人物的代码中进行一些修改
SCharacter.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "InputActionValue.h"
#include "SCharacter.generated.h"
UCLASS(config = Game)
class ACTIONROGUELIKE_API ASCharacter : public ACharacter
{
GENERATED_BODY()
UPROPERTY(VisibleAnywhere)
class USpringArmComponent* SpringArmComp;
UPROPERTY(VisibleAnywhere)
class UCameraComponent* CameraComp;
UPROPERTY(EditAnywhere, Category = Input)
class UInputMappingContext* DefaultMappingContext;
UPROPERTY(EditAnywhere, Category = Input)
class UInputAction* MoveAction;
UPROPERTY(EditAnywhere, Category = Input)
class UInputAction* JumpAction;
UPROPERTY(EditAnywhere, Category = Input)
class UInputAction* LookAction;
//增加一个输入事件,和之前的输入事件相同
UPROPERTY(EditAnywhere, Category = Input)
class UInputAction* PrimaryAttackAction;
//增加一个模板子类,类别为actor
protected:
UPROPERTY(EditAnywhere)
class TSubclassOf<AActor> ProjectileClass;
public:
// Sets default values for this character's properties
ASCharacter();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
protected:
void Move(const FInputActionValue& Value);
void Look(const FInputActionValue& Value);
//增加一个攻击函数
void PrimaryAttack();
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
SCharacter.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "SCharacter.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/Controller.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "GameFramework/CharacterMovementComponent.h"
// Sets default values
ASCharacter::ASCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
bUseControllerRotationPitch = false;
bUseControllerRotationRoll = false;
bUseControllerRotationYaw = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
SpringArmComp->SetupAttachment(RootComponent);
SpringArmComp->bUsePawnControlRotation = true;
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
CameraComp->SetupAttachment(SpringArmComp);
CameraComp->bUsePawnControlRotation = false;
}
// Called when the game starts or when spawned
void ASCharacter::BeginPlay()
{
Super::BeginPlay();
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
}
void ASCharacter::Move(const FInputActionValue& Value)
{
FVector2D MovementVector = Value.Get<FVector2D>();
if (Controller!=nullptr)
{
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(ForwardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}
void ASCharacter::Look(const FInputActionValue& Value)
{
FVector2D LookAxisVector = Value.Get<FVector2D>();
if (Controller!=nullptr)
{
AddControllerYawInput(LookAxisVector.X);
AddControllerPitchInput(LookAxisVector.Y);
}
}
//增加的攻击函数
void ASCharacter::PrimaryAttack()
{
//我们需要在函数内生成一个类对象,那么这个类对象除了类自身的属性以外,我们还需要给它一个在世界中的位置,也就是我们的transform
//我们需要给出创建的对象的位置和旋转角度,SpawnTM就是用控制器的旋转角度,玩家任务的中心位置来构成一个transform
FTransform SpawnTM = FTransform(GetControlRotation(), GetActorLocation());
//实例化一个actor类生成参数
FActorSpawnParameters SpawnParams;
//设置参数内的一个属性:如何处理生成的魔法弹和我们模型的重叠问题,这里斯坦福老师说的是我们直接生成对象就可以了,如果是DontSpawnIfColliding那么就不会生成对象
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
//在世界内生成对象,对象类型是Actor,参数就是我们之前说过的子模板类,生成的transform,生成对象的参数
GetWorld()->SpawnActor < AActor >(ProjectileClass, SpawnTM, SpawnParams);
}
// Called every frame
void ASCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void ASCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
{
//MoveAction
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ASCharacter::Move);
//JumpAction
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
//LookAction
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ASCharacter::Look);
//增加一个事件绑定
//PrimaryAttack
EnhancedInputComponent->BindAction(PrimaryAttackAction, ETriggerEvent::Triggered, this, &ASCharacter::PrimaryAttack);
}
}
以上工作完成之后,我们的人物就可以发射魔法弹了。随后老师给了一个作业,完成人物的跳跃以及爆炸桶的功能。跳跃功能我们在前面的时候就已经实现了(参照官方的第三人称代码实现的)所以我这里就实现爆炸桶的功能。
注:跟随斯坦福教程一步一步来,注意检测通道的设置,这个很重要!我自己用蓝图写游戏的时候在这里栽了很多次!
用C++还原教程的爆炸桶
SExplosiveBarrel.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SExplosiveBarrel.generated.h"
UCLASS()
class ACTIONROGUELIKE_API ASExplosiveBarrel : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ASExplosiveBarrel();
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)
class UStaticMeshComponent* StaticMeshComp;
//径向力组件(我个人的理解就是,这个组件可以模拟力的作用,爆炸产生的热能引起的气压变化产生的力(也许?)会影响到人,这个组件就模仿的这个力)
UPROPERTY(VisibleAnywhere)
class URadialForceComponent* RadialComp;
//我们要绑定的函数,此处的所有参数是参考蓝图的参数来定的,你可以一边看蓝图的事件节点一边看下面的函数声明,不加UFUNCTION()会报错
UFUNCTION()
void OnComponentHit(UPrimitiveComponent*HitComponent, AActor*OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, FHitResult Hit);
};
SExplosiveBarrel.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "SExplosiveBarrel.h"
#include "PhysicsEngine/RadialForceComponent.h"
#include "Components/StaticMeshComponent.h"
// Sets default values
ASExplosiveBarrel::ASExplosiveBarrel()
{
// 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;
//实例化一个委托
FScriptDelegate delegator;
//将我们在.h内定义的函数绑定到这个委托上来
delegator.BindUFunction(this, STATIC_FUNCTION_FNAME(TEXT("ASExplosiveBarrel::OnComponentHit")));
//实例化一个静态网格体
StaticMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComp"));
//下面这个相当于我们的event节点后面接了一个函数节点,这个函数节点就是我们自己声明的这个函数,所以当我们的静态网格体被击中的时候,就会触发这个事件,从而调用我们的函数
StaticMeshComp->OnComponentHit.Add(delegator);
//设置模拟物理属性为true
StaticMeshComp->SetSimulatePhysics(true);
//设置管道检测文档模板为PhysicsActor
StaticMeshComp->SetCollisionProfileName("PhysicsActor");
//设置为根组件
RootComponent = StaticMeshComp;
//实例化径向力组件
RadialComp = CreateDefaultSubobject<URadialForceComponent>(TEXT("RadioComp"));
//设置父组件为静态网格体组件
RadialComp->SetupAttachment(StaticMeshComp);
//设置径向力半径
RadialComp->Radius = 500.0f;
//设置径向力大小(力的大小)
RadialComp->ImpulseStrength = 2000.0f;
//如果为true,冲量将无视物体的重量,将造成固定的速度变化
RadialComp->bImpulseVelChange = true;
//组件是否在创建时激活,或必须时显示的激活(此处区别不大,教程把它关掉了,初步猜测为性能问题)
RadialComp->bAutoActivate = false;
}
// Called when the game starts or when spawned
void ASExplosiveBarrel::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ASExplosiveBarrel::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
//自定义函数的实现,拿到自身类里头的径向力组件,调用fireimpulse函数,该函数没有参数,直接调用就行。
void ASExplosiveBarrel::OnComponentHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, FHitResult Hit)
{
RadialComp->FireImpulse();
}
这里最大的问题说实话是将函数绑定到组件的事件上面,我们蓝图内直接连线,而C++需要一个委托才能绑定到我们的自定义函数,但是这个委托其实是一个标准流程,我们用蓝图自己整习惯了。