Stanford UE4 + C++ Course Learning Record 6: Character Jump and Powder Keg

Table of contents

1. Character Jump

2. Create the powder keg class

3. Create a blueprint class


1. Character Jump

        It is very simple to implement character jumping. There are already corresponding functions in the Character class, so you only need to add a line of code to the function that controls the input:

PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);

2. Create the powder keg class

        The after-school homework in the course requires that a powder keg be implemented in C++, which will explode and generate shock waves after being hit, and produce physical interaction with the surrounding Actors. First, you need to create the SurExplosiveBarrel class, the base class is AActor. This class contains two key components: MeshComp and ForceComp, which control the force field of the model and the explosion respectively. The UFUNCTION macro exposes functions in blueprints so that developers can call or extend functions directly from blueprints. The subsequent implementation part directly refers to the source code provided by the course. I don’t understand the calling method of OnActorHit() in the code for the time being. It will be added later (the content of this part has been added to Section 12. If the following code fails to run, please refer to Section 12. full code). The complete code is as follows:

// SurExplosiveBarrel.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SurExplosiveBarrel.generated.h"

class UStaticMeshComponent;
class URadialForceComponent;

UCLASS()
class SURKEAUE_API ASurExplosiveBarrel : public AActor
{
	GENERATED_BODY()
	
public:	
	
	ASurExplosiveBarrel();

protected:

	UPROPERTY(VisibleAnywhere)
	UStaticMeshComponent* MeshComp;

	UPROPERTY(VisibleAnywhere)
	URadialForceComponent* ForceComp;

	UFUNCTION()
	void OnActorHit();

};
// SurExplosiveBarrel.cpp
#include "SurExplosiveBarrel.h"
#include "PhysicsEngine/RadialForceComponent.h"
#include "Components/StaticMeshComponent.h"

// Sets default values
ASurExplosiveBarrel::ASurExplosiveBarrel()
{
 
	MeshComp = CreateDefaultSubobject<UStaticMeshComponent>("MeshComp");
	// UE中的“模拟物理”选项
	MeshComp->SetSimulatePhysics(true);
	// 等同于在UE中将“碰撞预设”设置为“PhysicsActor”
	MeshComp->SetCollisionProfileName(UCollisionProfile::PhysicsActor_ProfileName);
	RootComponent = MeshComp;

	ForceComp = CreateDefaultSubobject<URadialForceComponent>("ForceComp");
	ForceComp->SetupAttachment(MeshComp);

	ForceComp->Radius = 750.0f;			 // 爆炸范围
	ForceComp->ImpulseStrength = 700.0f; // 冲击力
	ForceComp->bImpulseVelChange = true; // 忽略质量大小;见UE中ForceComp的“冲量速度变更”
}

void ASurExplosiveBarrel::OnActorHit()
{
	ForceComp->FireImpulse();
}

3. Create a blueprint class

        Create an ExplosiveBarrel blueprint class in UE that inherits from SurExplosiveBarrel, and assign a mesh and material to him. Since there is no material for the red powder barrel in the project, a pink cylinder was replaced.

Figure 6-1 Creating a blueprint class

        Next drag it into the world, run the level and shoot magic particles at it. It can be found that the character and the cube next to it (need to set "Simulation Physics" to be turned on) were bounced off by the shock wave.

Figure 6-2 Running results

Guess you like

Origin blog.csdn.net/surkea/article/details/127120735