UE4 C++ small project summary

A cube rotating up and down

Project address: Portal
Operation process: Create a c + + c++c++ Class, toActor ActorA c t o r is the parent class, and then write the code,. H .h. h can write attributes, materials,.cpp .cpp. C P P implemented in the constructor andT ick () Tick ()T i c k ( ) function
final code:
F loating A ctor. H FloatingActor.hFloatingActor.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::T i c k ( f l o a t D e l t a T i m e )

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);

Player input and Pawn

Project address: Portal
Operation process: New P awn PawnP a w n category,.cpp .cppWrite the controller in . c p p ,. h .h. H in the preparation of the basic components,. Cpp .cpp. Create the camera and visible objects in c p p , configure the operation mapping and axis mapping of the game input, in. h .h. Write function declarations and variables in h , in.cpp .cpp. C the p- the p- in-written gameplay.
Final code:
M y P awn. H MyPawn.hM y P a w n . B

    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() A M y P a w n::A M y P a w n ( )

    // 将该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) 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 )

    // 根据"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) 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 tInputComponent)

    // 在按下或松开"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;
}

Guess you like

Origin blog.csdn.net/zhouzi2018/article/details/114653771