最简单的 UE 4 C++ 教程 —— 触发区域内的键盘响应开关灯【二十五】

举报
ShaderJoy 发表于 2021/11/18 23:33:04 2021/11/18
【摘要】 原教程是基于 UE 4.18,我是基于 UE 4.25】 英文原地址 接上一节教程,创建一个新的 C++ Actor 子类并将其命名为 LightSwitchPushButton 。我们将在头文件中定义四个东西 —— 我们将定义一个 UPointLightComponent、USphereComponent、...

原教程是基于 UE 4.18,我是基于 UE 4.25】

英文原地址

接上一节教程,创建一个新的 C++ Actor 子类并将其命名为 LightSwitchPushButton 。我们将在头文件中定义四个东西 —— 我们将定义一个 UPointLightComponentUSphereComponentfloat 变量和 void 函数。

下面是最终的头代码。

LightSwitchPushButton.h


  
  1. #pragma once
  2. #include "CoreMinimal.h"
  3. #include "GameFramework/Actor.h"
  4. #include "LightSwitchPushButton.generated.h"
  5. UCLASS()
  6. class UNREALCPP_API ALightSwitchPushButton : public AActor
  7. {
  8. GENERATED_BODY()
  9. public:
  10. // Sets default values for this actor's properties
  11. ALightSwitchPushButton();
  12. protected:
  13. // Called when the game starts or when spawned
  14. virtual void BeginPlay() override;
  15. public:
  16. // Called every frame
  17. // virtual void Tick(float DeltaTime) override;
  18. // declare point light comp
  19. UPROPERTY(VisibleAnywhere, Category = "Light Switch")
  20. class UPointLightComponent* PointLight;
  21. // declare sphere comp
  22. UPROPERTY(VisibleAnywhere, Category = "Light Switch")
  23. class USphereComponent* LightSphere;
  24. // declare light intensity variable
  25. UPROPERTY(VisibleAnywhere, Category = "Light Switch")
  26. float LightIntensity;
  27. // declare ToggleLight function
  28. UFUNCTION(BlueprintCallable, Category = "Light Switch")
  29. void ToggleLight();
  30. };

接下来,在我们的 .cpp 文件中,让我们首先 #include 我们将在代码中使用的必要脚本。包括Components / PointLightComponent.hComponents/ spherecomcomponent .h 两个文件。


  
  1. #include "LightSwitchPushButton.h"
  2. // include these header files
  3. #include "Components/PointLightComponent.h"
  4. #include "Components/SphereComponent.h"

我们将在构造函数中设置 actor 的所有默认属性。首先让我们设置我们的 float 变量 LightIntensity 为 3000.0f,它将使光相对于其他对象看起来足够明亮。接下来,我们将创建我们的UPointLightComponent 并将它设置为我们的 RootComponent 。之后,我们将创建USphereComponent,当我们的玩家在半径内时,它将作为碰撞球体。然后,我们将创建简单的ToggleLight() 函数来切换灯光的可见性状态。稍后我们将从玩家代码中调用该函数。下面是LightSwitchPushButton 角色的最后一个 .cpp 文件。

LightSwitchPushButton.cpp


  
  1. #include "LightSwitchPushButton.h"
  2. #include "Components/PointLightComponent.h"
  3. #include "Components/SphereComponent.h"
  4. // Sets default values
  5. ALightSwitchPushButton::ALightSwitchPushButton()
  6. {
  7. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  8. PrimaryActorTick.bCanEverTick = true;
  9. LightIntensity = 3000.0f;
  10. PointLight = CreateDefaultSubobject<UPointLightComponent>(TEXT("Point Light"));
  11. PointLight->Intensity = LightIntensity;
  12. //PointLight->bVisible = true; ///< 过时了
  13. //PointLight->SetVisibleFlag(true);
  14. PointLight->SetVisibility(true);
  15. RootComponent = PointLight;
  16. LightSphere = CreateDefaultSubobject<USphereComponent>(TEXT("Light Sphere Component"));
  17. LightSphere->InitSphereRadius(300.0f);
  18. LightSphere->SetCollisionProfileName(TEXT("Trigger"));
  19. LightSphere->SetCollisionResponseToChannel(ECC_Pawn, ECR_Ignore);
  20. LightSphere->SetupAttachment(RootComponent);
  21. }
  22. // Called when the game starts or when spawned
  23. void ALightSwitchPushButton::BeginPlay()
  24. {
  25. Super::BeginPlay();
  26. }
  27. void ALightSwitchPushButton::ToggleLight()
  28. {
  29. PointLight->ToggleVisibility();
  30. }

接下来,和上一节类似,让我们向项目添加一个 Action 输入。在本例中,我们将把 Action 输入绑定到键盘的 F键。转到 编辑>项目设置 ( Edit > Project Settings)。然后选择 Input 选项。单击 Action Mappings 旁边的加号。调用新的输入 Action 并从下拉菜单中选择 F

 

 

以下我们把目光转到 xxxCharacter.h / .cpp 上】 

 在 xxxCharacter.h文件中,在 OnFire 方法下添加 OnAction 方法。


  
  1. protected:
  2. /** Fires a projectile. */
  3. void OnFire();
  4. // on action
  5. void OnAction();

此外,我们还必须包含 LightSwitchPushButton 头文件,这样我们的角色才能访问它的功能。


  
  1. #include "CoreMinimal.h"
  2. #include "GameFramework/Character.h"
  3. // include our new LightSwitchPushButton header file
  4. #include "LightSwitchPushButton/LightSwitchPushButton.h"
  5. #include "UnrealCPPCharacter.generated.h"

然后为玩家当前重叠的灯开关声明一个变量 CurrentLightSwitch 。此外,我们还需要声明重叠事件,来使得当玩家处于光的球体组件的半径内时,触发我们想要运行的函数。


  
  1. // declare overlap begin function
  2. UFUNCTION()
  3. void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
  4. // declare overlap end function
  5. UFUNCTION()
  6. void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
  7. // declare current light switch
  8. class ALightSwitchPushButton* CurrentLightSwitch;

 同时还声明了 UCapsuleComponent 来处理我们的触发事件


  
  1. UPROPERTY(VisibleAnywhere, Category = "Trigger Capsule")
  2. class UCapsuleComponent* TriggerCapsule;

在构造函数中添加触发器胶囊并将其绑定到重叠事件。接着设置变量 CurrentLightSwitch NULL


  
  1. AUnrealCPPCharacter::AUnrealCPPCharacter()
  2. {
  3. ...
  4. // create trigger capsule
  5. TriggerCapsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Trigger Capsule"));
  6. TriggerCapsule->InitCapsuleSize(55.f, 96.0f);;
  7. TriggerCapsule->SetCollisionProfileName(TEXT("Trigger"));
  8. TriggerCapsule->SetupAttachment(RootComponent);
  9. // bind trigger events
  10. TriggerCapsule->OnComponentBeginOverlap.AddDynamic(this, &AUnrealCPPCharacter::OnOverlapBegin);
  11. TriggerCapsule->OnComponentEndOverlap.AddDynamic(this, &AUnrealCPPCharacter::OnOverlapEnd);
  12. // set current light switch to null
  13. CurrentLightSwitch = NULL;
  14. }

 

进一步,将 Action 输入绑定连接到玩家


  
  1. void AUnrealCPPCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
  2. {
  3. ...
  4. // Bind action event
  5. PlayerInputComponent->BindAction("Action", IE_Pressed, this, &AUnrealCPPCharacter::OnAction);
  6. }

OnAction() 函数添加到玩家脚本中。该函数将检查 CurrentLightSwitch 是否为 NULL 。如果CurrentLightSwitch 不为 NULL,那么当玩家按下动作键 F 时,将切换灯光的可见性(开关灯)。然后,添加重叠函数来设置和取消 CurrentLightSwitch

 


  
  1. void AUnrealCPPCharacter::OnAction()
  2. {
  3. if(CurrentLightSwitch)
  4. {
  5. CurrentLightSwitch->ToggleLight();
  6. }
  7. }
  8. // overlap on begin function
  9. void AUnrealCPPCharacter::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
  10. {
  11. if (OtherActor && (OtherActor != this) && OtherComp && OtherActor->GetClass()->IsChildOf(ALightSwitchPushButton::StaticClass()))
  12. {
  13. CurrentLightSwitch = Cast<ALightSwitchPushButton>(OtherActor);
  14. }
  15. }
  16. // overlap on end function
  17. void AUnrealCPPCharacter::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
  18. {
  19. if (OtherActor && (OtherActor != this) && OtherComp)
  20. {
  21. CurrentLightSwitch = NULL;
  22. }
  23. }

编译代码。拖放 actor (LightSwitchPushButton)到场景中,当玩家进入球形触发区域,点击 F 键开关灯。

最后的效果图如下

文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。

原文链接:panda1234lee.blog.csdn.net/article/details/119204101

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。