UE4 C++ 小项目汇总

一个旋转上下的立方体

项目地址:传送门
操作流程:创建一个 c + + c++ c++类,以 A c t o r Actor Actor为父类,然后编写代码, . h .h .h中可以写点属性,材质, . c p p .cpp .cpp中实现构造函数和 T i c k ( ) Tick() Tick()函数
最终代码:
F l o a t i n g A c t o r . h FloatingActor.h FloatingActor.h

//声明的是 StaticMeshComponent,它将担当对象的视觉表示。
public:
	UPROPERTY(VisibleAnywhere)
	UStaticMeshComponent* VisualMesh;
	

F l o a t i n g A c t o r . c p p FloatingActor.cpp FloatingActor.cpp

//在VisualMesh引用中填充新的StaticMeshComponent、 将它附加到Actor
VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
VisualMesh->SetupAttachment(RootComponent);
//将它设为 初学者内容包 资源中的立方体模型
static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));

if (CubeVisualAsset.Succeeded())
{
    
    
    VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
    VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
}

A F l o a t i n g A c t o r : : T i c k ( f l o a t D e l t a T i m e ) AFloatingActor::Tick(float DeltaTime) AFloatingActor::Tick(floatDeltaTime)

FVector NewLocation = GetActorLocation();
FRotator NewRotation = GetActorRotation();
float RunningTime = GetGameTimeSinceCreation();
float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
NewLocation.Z += DeltaHeight * 20.0f;       //Scale our height by a factor of 20
float DeltaRotation = DeltaTime * 20.0f;    //Rotate by 20 degrees per second
NewRotation.Yaw += DeltaRotation;
SetActorLocationAndRotation(NewLocation, NewRotation);

玩家输入和Pawn

项目地址:传送门
操作流程:新建 P a w n Pawn Pawn类, . c p p .cpp .cpp中编写控制器, . h .h .h中编写基本组件, . c p p .cpp .cpp中创建相机和可见对象,配置游戏输入的操作映射和轴映射后,在 . h .h .h中写好函数声明和变量,在 . c p p .cpp .cpp中写好游戏操作。
最终代码:
M y P a w n . h MyPawn.h MyPawn.h

    UPROPERTY(EditAnywhere)
    USceneComponent* OurVisibleComponent;

    //输入函数
    void Move_XAxis(float AxisValue);
    void Move_YAxis(float AxisValue);
    void StartGrowing();
    void StopGrowing();

    //输入变量
    FVector CurrentVelocity;
    bool bGrowing;

A M y P a w n : : A M y P a w n ( ) AMyPawn::AMyPawn() AMyPawn::AMyPawn()

    // 将该pawn设为由最小编号玩家控制
    AutoPossessPlayer = EAutoReceiveInput::Player0;

    // 创建可附加内容的虚拟根组件。
    RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
    // 创建相机和可见对象
    UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
    OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent"));
    // 将相机和可见对象附加到根组件。偏移并旋转相机。
    OurCamera->SetupAttachment(RootComponent);
    OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
    OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
    OurVisibleComponent->SetupAttachment(RootComponent);

v o i d A M y P a w n : : T i c k ( f l o a t D e l t a T i m e ) void AMyPawn::Tick(float DeltaTime) voidAMyPawn::Tick(floatDeltaTime)

    // 根据"Grow"操作处理增长和缩减
    {
    
    
        float CurrentScale = OurVisibleComponent->GetComponentScale().X;
        if (bGrowing)
        {
    
    
            // 一秒内增长到两倍大小
            CurrentScale += DeltaTime;
        }
        else
        {
    
    
            // 以增长速度缩减一半
            CurrentScale -= (DeltaTime * 0.5f);
        }
        // 确保不会降至初始大小以下,或者增至两倍大小以上。
        CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
        OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
    }

    // 根据"MoveX"和"MoveY"轴处理移动
    {
    
    
        if (!CurrentVelocity.IsZero())
        {
    
    
            FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
            SetActorLocation(NewLocation);
        }
    }

v o i d A M y P a w n : : S e t u p P l a y e r I n p u t C o m p o n e n t ( c l a s s U I n p u t C o m p o n e n t ∗ I n p u t C o m p o n e n t ) void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent) voidAMyPawn::SetupPlayerInputComponent(classUInputComponentInputComponent)

    // 在按下或松开"Grow"键时做出响应。
    InputComponent->BindAction("Grow", IE_Pressed, this, &AMyPawn::StartGrowing);
    InputComponent->BindAction("Grow", IE_Released, this, &AMyPawn::StopGrowing);

    // 对两个移动轴"MoveX"和"MoveY"的值逐帧反应。
    InputComponent->BindAxis("MoveX", this, &AMyPawn::Move_XAxis);
    InputComponent->BindAxis("MoveY", this, &AMyPawn::Move_YAxis);
void AMyPawn::Move_XAxis(float AxisValue)
{
    
    
    // 以100单位/秒的速度向前或向后移动
    CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}

void AMyPawn::Move_YAxis(float AxisValue)
{
    
    
    // 以100单位/秒的速度向右或向左移动
    CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}

void AMyPawn::StartGrowing()
{
    
    
    bGrowing = true;
}

void AMyPawn::StopGrowing()
{
    
    
    bGrowing = false;
}

猜你喜欢

转载自blog.csdn.net/zhouzi2018/article/details/114653771