https://docs.unrealengine.com/latest/CHN/index.html

中编程指南

FloatingActor.h

  1. #pragma once
  2. #include "GameFramework/Actor.h"
  3. #include "FloatingActor.generated.h"
  4. UCLASS()
  5. class QUICKSTART_API AFloatingActor : public AActor
  6. {
  7. GENERATED_BODY()
  8. public:
  9. // 设置此actor属性的默认值
  10. AFloatingActor();
  11. virtual void BeginPlay() override;
  12. virtual void Tick( float DeltaSeconds ) override;
  13. float RunningTime;
  14. };

FloatingActor.cpp

  1. #include "QuickStart.h"
  2. #include "FloatingActor.h"
  3.  
  4. // 设置默认值
  5. AFloatingActor::AFloatingActor()
  6. {
  7. // 将此actor设置为在每一帧都调用Tick()。 如果您不需要这项功能,您可以关闭它以改善性能。
  8. PrimaryActorTick.bCanEverTick = true;
  9. }
  10.  
  11. // 当游戏开始或生成时调用
  12. void AFloatingActor::BeginPlay()
  13. {
  14. Super::BeginPlay();
  15. }
  16.  
  17. // 在每一帧调用
  18. void AFloatingActor::Tick( float DeltaTime )
  19. {
  20. Super::Tick( DeltaTime );
  21.  
  22. FVector NewLocation = GetActorLocation();
  23. float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
  24. NewLocation.Z += DeltaHeight * 20.0f; //把高度以20的系数进行缩放
  25. RunningTime += DeltaTime;
  26. SetActorLocation(NewLocation);
  27. }

获取设置本地位置

1 GetActorLocation(),SetActorLocation(FVector v)


数学函数库

2 FMath


3 UCLASS() – 告知虚幻引擎生成类的反射数据。类必须派生自 UObject。
USTRUCT() – 告知虚幻引擎生成结构体的反射数据。
GENERATED_BODY() – UE4 使用它替代为类型生成的所有必需样板文件代码。
UPROPERTY() – 使 UCLASS 或 USTRUCT 的成员变量可用作 UPROPERTY。UPROPERTY 用途广泛。它允许变量被复制、被序列化,并可从蓝图中进行访问。垃圾回收器还使用它们来追踪对 UObject 的引用数。

UFUNCTION() – 使 UCLASS 或 USTRUCT 的类方法可用作 UFUNCTION。UFUNCTION 允许类方法从蓝图中被调用,并在其他资源中用作 RPC。

MyPawn.h
  1. #pragma once
  2.  
  3. #include "GameFramework/Pawn.h"
  4. #include "MyPawn.generated.h"
  5.  
  6. UCLASS()
  7. class HOWTO_PLAYERINPUT_API AMyPawn : public APawn
  8. {
  9. GENERATED_BODY()
  10. public:
  11. // 设置默认值
  12. AMyPawn();
  13. // 当游戏开始或生成时调用
  14. virtual void BeginPlay() override;
  15. // 在每一帧调用
  16. virtual void Tick(float DeltaSeconds) override;
  17. // 调用以绑定功能到输入
  18. virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;// nafio info 这个是override的
  19. UPROPERTY(EditAnywhere)
  20. USceneComponent* OurVisibleComponent;
  21. // 输入函数
  22. void Move_XAxis(float AxisValue);
  23. void Move_YAxis(float AxisValue);
  24. void StartGrowing();
  25. void StopGrowing();
  26. //输入变量
  27. FVector CurrentVelocity;
  28. bool bGrowing;
  29. };
  30.  

MyPawn.cpp

  1. #include "HowTo_PlayerInput.h"
  2. #include "MyPawn.h"
  3.  
  4. // 设置默认值
  5. AMyPawn::AMyPawn()
  6. {
  7. // 将此pawn设置为在每一帧都调用Tick()。 如果您不需要这项功能,您可以关闭它以改善性能。
  8. PrimaryActorTick.bCanEverTick = true;
  9.  
  10. // 将此pawn设置为由最低数量的玩家进行控制
  11. AutoPossessPlayer = EAutoReceiveInput::Player0;
  12.  
  13. // 创建一个可供添加对象的空根组件。
  14. RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
  15. // 创建相机和可见项目
  16. UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
  17. OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent"));
  18. // 附加相机和可见对象到根组件。 偏移并旋转相机。
  19. OurCamera->AttachTo(RootComponent);
  20. OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
  21. OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
  22. OurVisibleComponent->AttachTo(RootComponent);
  23. }
  24.  
  25. // 当游戏开始或生成时调用
  26. void AMyPawn::BeginPlay()
  27. {
  28. Super::BeginPlay();
  29.  
  30. }
  31.  
  32. // 在每一帧调用
  33. void AMyPawn::Tick(float DeltaTime)
  34. {
  35. Super::Tick(DeltaTime);
  36.  
  37. // 基于"Grow"操作来处理增长和收缩
  38. {
  39. float CurrentScale = OurVisibleComponent->GetComponentScale().X;
  40. if (bGrowing)
  41. {
  42. // 在一秒的时间内增长到两倍的大小
  43. CurrentScale += DeltaTime;
  44. }
  45. else
  46. {
  47. // 随着增长收缩到一半
  48. CurrentScale -= (DeltaTime * 0.5f);
  49. }
  50. // 确认永不低于起始大小,或增大之前的两倍大小。
  51. CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
  52. OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
  53. }
  54.  
  55. // 基于"MoveX"和 "MoveY"坐标轴来处理移动
  56. {
  57. if (!CurrentVelocity.IsZero())
  58. {
  59. FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
  60. SetActorLocation(NewLocation);
  61. }
  62. }
  63. }
  64.  
  65. // 调用以绑定功能到输入
  66. void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
  67. {
  68. Super::SetupPlayerInputComponent(InputComponent);
  69.  
  70. // 在按下或松开"Grow"键时进行响应。
  71. InputComponent->BindAction("Grow", IE_Pressed, this, &AMyPawn::StartGrowing);
  72. InputComponent->BindAction("Grow", IE_Released, this, &AMyPawn::StopGrowing);
  73.  
  74. // 在每一帧都对两个移动坐标轴的值进行响应,它们分别是"MoveX"和"MoveY"。
  75. InputComponent->BindAxis("MoveX", this, &AMyPawn::Move_XAxis);
  76. InputComponent->BindAxis("MoveY", this, &AMyPawn::Move_YAxis);
  77. }
  78.  
  79. void AMyPawn::Move_XAxis(float AxisValue)
  80. {
  81. // 以每秒100单位的速度向前或向后移动
  82. CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
  83. }
  84.  
  85. void AMyPawn::Move_YAxis(float AxisValue)
  86. {
  87. // 以每秒100单位的速度向右或向左移动
  88. CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
  89. }
  90.  
  91. void AMyPawn::StartGrowing()
  92. {
  93. bGrowing = true;
  94. }
  95.  
  96. void AMyPawn::StopGrowing()
  97. {
  98. bGrowing = false;
  99. }

1 SetupPlayerInputCompnent复写了pawn的方法,用于注册输入

actor->pawn(加了接收输入信息方法)->character(加了走动)

输入绑定略…


2 AutoPossessPlayer = EAutoReceiveInput::Player0;

这句翻译应该有问题,应该是由player0输入来控制当前这个pawn


3 DeltaTime一次循环时间间隔

  1. //Find the actor that handles control for the local player.
  2. APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);
  3. OurPlayerController->GetViewTarget();//获取当前Controller相机
  4. OurPlayerController->SetViewTarget(CameraOne);//直接切换到相机1
  5. OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);//平滑切换到相机2
获取controller原型
  1. static APlayerController * GetPlayerController
  2. (
  3. const UObject * WorldContextObject,
  4. int32 PlayerIndex
  5. )

Returns the player controller at the specified player index
第一个UObject类型参数这里传的是actor获取的就是这个actor的第index个controller

Countdown.h
  1. #pragma once
  2.  
  3. #include "GameFramework/Actor.h"
  4. #include "Countdown.generated.h"
  5.  
  6. UCLASS()
  7. class HOWTO_VTE_API ACountdown : public AActor
  8. {
  9. GENERATED_BODY()
  10.  
  11. public:
  12. // 设置该 actor 属性的默认值
  13. ACountdown();
  14.  
  15. // 游戏开始时或生成时调用
  16. virtual void BeginPlay() override;
  17.  
  18. // 每帧调用
  19. virtual void Tick( float DeltaSeconds ) override;
  20.  
  21. //倒计时运行时长,按秒计
  22. UPROPERTY(EditAnywhere)
  23. int32 CountdownTime;
  24.  
  25. UTextRenderComponent* CountdownText;
  26.  
  27. void UpdateTimerDisplay();
  28.  
  29. void AdvanceTimer();
  30.  
  31. UFUNCTION(BlueprintNativeEvent)
  32. void CountdownHasFinished();
  33. virtual void CountdownHasFinished_Implementation();
  34.  
  35. FTimerHandle CountdownTimerHandle;
  36. };

Countdown.cpp

  1. #include "HowTo_VTE.h"
  2. #include "Countdown.h"
  3.  
  4. // 设置默认值
  5. ACountdown::ACountdown()
  6. {
  7. // 将此 actor 设为每帧调用 Tick()。不需要时可将此关闭,以提高性能。
  8. PrimaryActorTick.bCanEverTick = false;
  9.  
  10. CountdownText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CountdownNumber"));
  11. CountdownText->SetHorizontalAlignment(EHTA_Center);
  12. CountdownText->SetWorldSize(150.0f);
  13. RootComponent = CountdownText;
  14.  
  15. CountdownTime = 3;
  16. }
  17.  
  18. // 游戏开始时或生成时调用
  19. void ACountdown::BeginPlay()
  20. {
  21. Super::BeginPlay();
  22.  
  23. UpdateTimerDisplay();
  24. GetWorldTimerManager().SetTimer(CountdownTimerHandle, this, &ACountdown::AdvanceTimer, 1.0f, true);
  25. }
  26.  
  27. // 每帧调用
  28. void ACountdown::Tick( float DeltaTime )
  29. {
  30. Super::Tick( DeltaTime );
  31.  
  32. }
  33.  
  34. void ACountdown::UpdateTimerDisplay()
  35. {
  36. CountdownText->SetText(FString::FromInt(FMath::Max(CountdownTime, 0)));
  37. }
  38.  
  39. void ACountdown::AdvanceTimer()
  40. {
  41. --CountdownTime;
  42. UpdateTimerDisplay();
  43. if (CountdownTime < 1)
  44. {
  45. // 倒计时结束,停止运行定时器。
  46. GetWorldTimerManager().ClearTimer(CountdownTimerHandle);
  47. //在定时器结束时按需要执行特殊操作。
  48. CountdownHasFinished();
  49. }
  50. }
  51.  
  52. void ACountdown::CountdownHasFinished_Implementation()
  53. {
  54. //改为一个特殊的读出
  55. CountdownText->SetText(TEXT("GO!"));
  56. }

1 FTimerHandle CountdownTimerHandle;定时器

调用方法
2 BeginPlay中设置定时器事件
GetWorldTimerManager().SetTimer(CountdownTimerHandle, this, &ACountdown::AdvanceTimer, 1.0f, true);

3 定时执行AdvanceTimer函数

4 手动计时,到达后使用
 GetWorldTimerManager().ClearTimer(CountdownTimerHandle);

停止计时器

输入绑定略
PawnWithCamera.h
  1. #pragma once
  2.  
  3. #include "GameFramework/Pawn.h"
  4. #include "PawnWithCamera.generated.h"
  5.  
  6. UCLASS()
  7. class HOWTO_PLAYERCAMERA_API APawnWithCamera : public APawn
  8. {
  9. GENERATED_BODY()
  10.  
  11. public:
  12. // 设置此pawn属性的默认值
  13. APawnWithCamera();
  14.  
  15. // 当游戏开始或生成时调用
  16. virtual void BeginPlay() override;
  17.  
  18. // 在每一帧调用
  19. virtual void Tick( float DeltaSeconds ) override;
  20.  
  21. // 调用以绑定功能到输入
  22. virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
  23.  
  24. protected:
  25. UPROPERTY(EditAnywhere)
  26. USpringArmComponent* OurCameraSpringArm;
  27. UCameraComponent* OurCamera;
  28.  
  29. //输入变量
  30. FVector2D MovementInput;
  31. FVector2D CameraInput;
  32. float ZoomFactor;
  33. bool bZoomingIn;
  34.  
  35. // 输入函数
  36. void MoveForward(float AxisValue);
  37. void MoveRight(float AxisValue);
  38. void PitchCamera(float AxisValue);
  39. void YawCamera(float AxisValue);
  40. void ZoomIn();
  41. void ZoomOut();
  42. };

PawnWithCamera.cpp

  1. #include "HowTo_PlayerCamera.h"
  2. #include "PawnWithCamera.h"
  3.  
  4. // 设置默认值
  5. APawnWithCamera::APawnWithCamera()
  6. {
  7. // 将此pawn设置为在每一帧都调用Tick()。 如果您不需要这项功能,您可以关闭它以改善性能。
  8. PrimaryActorTick.bCanEverTick = true;
  9.  
  10. //创建组件
  11. RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
  12. OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
  13. OurCameraSpringArm->AttachTo(RootComponent);
  14. OurCameraSpringArm->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 50.0f), FRotator(-60.0f, 0.0f, 0.0f));
  15. OurCameraSpringArm->TargetArmLength = 400.f;
  16. OurCameraSpringArm->bEnableCameraLag = true;
  17. OurCameraSpringArm->CameraLagSpeed = 3.0f;
  18. OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));
  19. OurCamera->AttachTo(OurCameraSpringArm, USpringArmComponent::SocketName);
  20.  
  21. //控制默认玩家
  22. AutoPossessPlayer = EAutoReceiveInput::Player0;
  23. }
  24.  
  25. // 当游戏开始或生成时调用
  26. void APawnWithCamera::BeginPlay()
  27. {
  28. Super::BeginPlay();
  29.  
  30. }
  31.  
  32. // 在每一帧调用
  33. void APawnWithCamera::Tick( float DeltaTime )
  34. {
  35. Super::Tick(DeltaTime);
  36.  
  37. //如果按下了放大按钮则放大,否则就缩小
  38. {
  39. if (bZoomingIn)
  40. {
  41. ZoomFactor += DeltaTime / 0.5f; //Zoom in over half a second
  42. }
  43. else
  44. {
  45. ZoomFactor -= DeltaTime / 0.25f; //Zoom out over a quarter of a second
  46. }
  47. ZoomFactor = FMath::Clamp<float>(ZoomFactor, 0.0f, 1.0f);
  48. // 基于ZoomFactor来混合相机的视域和弹簧臂的长度
  49. OurCamera->FieldOfView = FMath::Lerp<float>(90.0f, 60.0f, ZoomFactor);
  50. OurCameraSpringArm->TargetArmLength = FMath::Lerp<float>(400.0f, 300.0f, ZoomFactor);
  51. }
  52.  
  53. //选择actor的偏转,这样将会旋转相机,因为相机附着于actor
  54. {
  55. FRotator NewRotation = GetActorRotation();
  56. NewRotation.Yaw += CameraInput.X;
  57. SetActorRotation(NewRotation);
  58. }
  59.  
  60. // 选择相机的倾斜,但对其进行限制,这样我们将总是向下看
  61. {
  62. FRotator NewRotation = OurCameraSpringArm->GetComponentRotation();
  63. NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);
  64. OurCameraSpringArm->SetWorldRotation(NewRotation);
  65. }
  66.  
  67. // 基于"MoveX"和 "MoveY"轴来处理移动
  68. {
  69. if (!MovementInput.IsZero())
  70. {
  71. // 把移动输入轴的值每秒缩放100个单位
  72. MovementInput = MovementInput.SafeNormal() * 100.0f;
  73. FVector NewLocation = GetActorLocation();
  74. NewLocation += GetActorForwardVector() * MovementInput.X * DeltaTime;
  75. NewLocation += GetActorRightVector() * MovementInput.Y * DeltaTime;
  76. SetActorLocation(NewLocation);
  77. }
  78. }
  79. }
  80.  
  81. // 调用以绑定功能到输入
  82. void APawnWithCamera::SetupPlayerInputComponent(class UInputComponent* InputComponent)
  83. {
  84. Super::SetupPlayerInputComponent(InputComponent);
  85.  
  86. // 绑定事件到"ZoomIn"
  87. InputComponent->BindAction("ZoomIn", IE_Pressed, this, &APawnWithCamera::ZoomIn);
  88. InputComponent->BindAction("ZoomIn", IE_Released, this, &APawnWithCamera::ZoomOut);
  89.  
  90. //为四条轴绑定每帧的处理
  91. InputComponent->BindAxis("MoveForward", this, &APawnWithCamera::MoveForward);
  92. InputComponent->BindAxis("MoveRight", this, &APawnWithCamera::MoveRight);
  93. InputComponent->BindAxis("CameraPitch", this, &APawnWithCamera::PitchCamera);
  94. InputComponent->BindAxis("CameraYaw", this, &APawnWithCamera::YawCamera);
  95. }
  96.  
  97. // 输入函数
  98. void APawnWithCamera::MoveForward(float AxisValue)
  99. {
  100. MovementInput.X = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
  101. }
  102.  
  103. void APawnWithCamera::MoveRight(float AxisValue)
  104. {
  105. MovementInput.Y = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
  106. }
  107.  
  108. void APawnWithCamera::PitchCamera(float AxisValue)
  109. {
  110. CameraInput.Y = AxisValue;
  111. }
  112.  
  113. void APawnWithCamera::YawCamera(float AxisValue)
  114. {
  115. CameraInput.X = AxisValue;
  116. }
  117.  
  118. void APawnWithCamera::ZoomIn()
  119. {
  120. bZoomingIn = true;
  121. }
  122.  
  123. void APawnWithCamera::ZoomOut()
  124. {
  125. bZoomingIn = false;
  126. }

1 创建组件,RootCompnent是Actore的根节点,USceneComponent应该类似Transform,空节点,TEXT后面是组件节点名称

 RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));

类似的有,创建弹簧臂组件

OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));



2 绑定到父节点,AttachTo
OurCameraSpringArm->AttachTo(RootComponent);


3 获取设置Actor相关属性
GetActorRotation();
SetActorRotation(NewRotation);


4 四元数旋转操作
 FRotator NewRotation = OurCameraSpringArm->GetComponentRotation();
 NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);
 OurCameraSpringArm->SetWorldRotation(NewRotation);


5 FVector2获取单位向量,3维向量是FVector
 FVector2 MovementInput = MovementInput.SafeNormal()


6 获取Actor 朝向相关
GetActorForwardVector()
GetActorRightVector()

CollidingPawn.h

// 版权所有 1998-2017 Epic Games, Inc. 保留所有权利。

#pragma once

#include "GameFramework/Pawn.h"
#include "CollidingPawn.generated.h"

UCLASS()
class HOWTO_COMPONENTS_API ACollidingPawn : public APawn
{
    GENERATED_BODY()

public:
    // 设置该 pawn 属性的默认值
    ACollidingPawn();

    // 游戏开始时或生成时调用
    virtual void BeginPlay() override;

    // 每帧调用
    virtual void Tick( float DeltaSeconds ) override;

    // 调用后将功能绑定到输入
    virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;

    UParticleSystemComponent* OurParticleSystem;
    class UCollidingPawnMovementComponent* OurMovementComponent;

    virtual UPawnMovementComponent* GetMovementComponent() const override;

    void MoveForward(float AxisValue);
    void MoveRight(float AxisValue);
    void Turn(float AxisValue);
    void ParticleToggle();
};

CollidingPawn.cpp

// 版权所有 1998-2017 Epic Games, Inc. 保留所有权利。

#include "HowTo_Components.h"
#include "CollidingPawn.h"
#include "CollidingPawnMovementComponent.h"

// 设置默认值
ACollidingPawn::ACollidingPawn()
{
    // 将此 pawn 设为每帧调用 Tick()。不需要时可将此关闭,以提高性能。
    PrimaryActorTick.bCanEverTick = true;

    // 我们的根组件是对物理作出反应的球体
    USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
    RootComponent = SphereComponent;
    SphereComponent->InitSphereRadius(40.0f);
    SphereComponent->SetCollisionProfileName(TEXT("Pawn"));

    // 创建并放置一个网格体组件,以便了解球体的所在位置
    UStaticMeshComponent* SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
    SphereVisual->AttachTo(RootComponent);
    static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
    if (SphereVisualAsset.Succeeded())
    {
        SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
        SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
        SphereVisual->SetWorldScale3D(FVector(0.8f));
    }

    // 创建一个可启用或停用的粒子系统
    OurParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MovementParticles"));
    OurParticleSystem->AttachTo(SphereVisual);
    OurParticleSystem->bAutoActivate = false;
    OurParticleSystem->SetRelativeLocation(FVector(-20.0f, 0.0f, 20.0f));
    static ConstructorHelpers::FObjectFinder<UParticleSystem> ParticleAsset(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));
    if (ParticleAsset.Succeeded())
    {
        OurParticleSystem->SetTemplate(ParticleAsset.Object);
    }

    // 使用弹簧臂让摄像机运动平稳而自然。
    USpringArmComponent* SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraAttachmentArm"));
    SpringArm->AttachTo(RootComponent);
    SpringArm->RelativeRotation = FRotator(-45.f, 0.f, 0.f);
    SpringArm->TargetArmLength = 400.0f;
    SpringArm->bEnableCameraLag = true;
    SpringArm->CameraLagSpeed = 3.0f;

    // 创建一个摄像机,将其附着到弹簧臂
    UCameraComponent* Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("ActualCamera"));
    Camera->AttachTo(SpringArm, USpringArmComponent::SocketName);

    // 掌控默认玩家
    AutoPossessPlayer = EAutoReceiveInput::Player0;

    // 创建移动组件的一个实例,并告知其更新根组件。
    OurMovementComponent = CreateDefaultSubobject<UCollidingPawnMovementComponent>(TEXT("CustomMovementComponent"));
    OurMovementComponent->UpdatedComponent = RootComponent;
}

// 游戏开始时或生成时调用
void ACollidingPawn::BeginPlay()
{
    Super::BeginPlay();

}

// 每帧调用
void ACollidingPawn::Tick( float DeltaTime )
{
    Super::Tick( DeltaTime );

}

// 调用后将功能绑定到输入
void ACollidingPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
    Super::SetupPlayerInputComponent(InputComponent);

    InputComponent->BindAction("ParticleToggle", IE_Pressed, this, &ACollidingPawn::ParticleToggle);

    InputComponent->BindAxis("MoveForward", this, &ACollidingPawn::MoveForward);
    InputComponent->BindAxis("MoveRight", this, &ACollidingPawn::MoveRight);
    InputComponent->BindAxis("Turn", this, &ACollidingPawn::Turn);
}

UPawnMovementComponent* ACollidingPawn::GetMovementComponent() const
{
    return OurMovementComponent;
}

void ACollidingPawn::MoveForward(float AxisValue)
{
    if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
    {
        OurMovementComponent->AddInputVector(GetActorForwardVector() * AxisValue);
    }
}

void ACollidingPawn::MoveRight(float AxisValue)
{
    if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
    {
        OurMovementComponent->AddInputVector(GetActorRightVector() * AxisValue);
    }
}

void ACollidingPawn::Turn(float AxisValue)
{
    FRotator NewRotation = GetActorRotation();
    NewRotation.Yaw += AxisValue;
    SetActorRotation(NewRotation);
}

void ACollidingPawn::ParticleToggle()
{
    if (OurParticleSystem && OurParticleSystem->Template)
    {
        OurParticleSystem->ToggleActive();
    }
}

CollidingPawnMovementComponent.h

// 版权所有 1998-2017 Epic Games, Inc. 保留所有权利。

#pragma once

#include "GameFramework/PawnMovementComponent.h"
#include "CollidingPawnMovementComponent.generated.h"

/**
 * 
 */
UCLASS()
class HOWTO_COMPONENTS_API UCollidingPawnMovementComponent : public UPawnMovementComponent
{
    GENERATED_BODY()

public:
    virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;  
};

CollidingPawnMovementComponent.cpp

// 版权所有 1998-2017 Epic Games, Inc. 保留所有权利。

#include "HowTo_Components.h"
#include "CollidingPawnMovementComponent.h"

void UCollidingPawnMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
    Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

    // 确保所有内容仍然有效,并允许移动。
    if (!PawnOwner || !UpdatedComponent || ShouldSkipUpdate(DeltaTime))
    {
        return;
    }

    // 获取(然后清除)在 ACollidingPawn::Tick 设置的移动矢量。
    FVector DesiredMovementThisFrame = ConsumeInputVector().GetClampedToMaxSize(1.0f) * DeltaTime * 150.0f;
    if (!DesiredMovementThisFrame.IsNearlyZero())
    {
        FHitResult Hit;
        SafeMoveUpdatedComponent(DesiredMovementThisFrame, UpdatedComponent->GetComponentRotation(), true, Hit);

        // 如碰到物体,尝试沿其滑动
        if (Hit.IsValidBlockingHit())
        {
            SlideAlongSurface(DesiredMovementThisFrame, 1.f - Hit.Time, Hit.Normal, Hit);
        }
    }
};

1 创建一个静态网格组件,并使用资源填充
// 创建并放置一个网格体组件,以便了解球体的所在位置
    UStaticMeshComponent* SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
    SphereVisual->AttachTo(RootComponent);
    static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
    if (SphereVisualAsset.Succeeded())
    {
        SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
        SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
        SphereVisual->SetWorldScale3D(FVector(0.8f));
    }

2 创建一个粒子组件,并用外部资源填充


 OurParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MovementParticles"));
    OurParticleSystem->AttachTo(SphereVisual);
    OurParticleSystem->bAutoActivate = false;
    OurParticleSystem->SetRelativeLocation(FVector(-20.0f, 0.0f, 20.0f));
    static ConstructorHelpers::FObjectFinder<UParticleSystem> ParticleAsset(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));
    if (ParticleAsset.Succeeded())
    {
        OurParticleSystem->SetTemplate(ParticleAsset.Object);
    }


具体运动组件部分暂时略过。

版权声明:本文为nafio原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/nafio/p/9137081.html