UE4学习笔记4th:编程和绑定游戏操作

在这一节中,我们来处理对输入的响应

双击打开MyPawn在vs中进行编辑。
这里写图片描述

在MypPawn.h中添加以下定义:

    void Move_XAxis(float AxisValue);
    void Move_YAxis(float AxisValue);
    void StartGrowing();
    void StopGrowing();
    //输入变量
    FVector CurrentVelocity;
    bool bGrowing;

这里写图片描述

前两个用于前后左右方向上的处理,后两个处理跳跃。
它们在运行时,会改变变量中的值,从而确定在游戏中应执行的操作。

切换到MyPawn.cpp,对这四个函数进行编程。
在最下方输入

void AMyPawn::Move_XAxis(float AxisValue)
{
    //前后
    CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f)*100.0f;
}

void AMyPawn::Move_YAxis(float AxisValue)
{
    //左右
    CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f)*100.0f;

}

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

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

这里写图片描述

现在我们已经完成了对输入函数的定义,接下来要将它们进行绑定

在AMyPawn::SetupPlayerInputComponent函数中输入以下代码:

//响应Grow键
InputComponent->BindAction("Grow",IE_Pressed,this,&AMyPawn::StartGrowing);
InputComponent->BindAction("Grow",IE_Released,this,&AMyPawn::StopGrowing);

//响应两个坐标轴值
InputComponent->BindAxis("MoveX", this, &AMyPawn::Move_XAxis);
InputComponent->BindAxis("MoveY", this, &AMyPawn::Move_YAxis);

现在我完成了对输入的绑定,接下来我要将它们在视口中体现出来。
在AMyPawn::Tick中添加如下代码:

//基于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 NewLoaction = GetActorLocation() + (CurrentVelocity*DeltaTime);
            SetActorLocation(NewLoaction);
        }
    }

完成后编译,效果如下
这里写图片描述

这里写图片描述
编译

编译完代码后,在编辑器中按下 Play ,应该可以使用WASD键来控制 Pawn ,而且我们应该可以通过按住空格键来使之增大,并在松开后看着其缩小。

下面是完整的代码:

MyPawn.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

UCLASS()
class HOWTO_PLAYERINPUT_API AMyPawn : public APawn
{
    GENERATED_BODY()

public:
    // Sets default values for this pawn's properties
    AMyPawn();

    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

    // Called every frame
    virtual void Tick( float DeltaSeconds ) override;

    // Called to bind functionality to input
    virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;

    UPROPERTY(EditAnywhere)
        USceneComponent* OurVisibleComponent;
    //输入函数
    void Move_XAxis(float AxisValue);
    void Move_YAxis(float AxisValue);
    void StartGrowing();
    void StopGrowing();
    //输入变量
    FVector CurrentVelocity;
    bool bGrowing;

};


MyPawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.

#include "HowTo_PlayerInput.h"
#include "MyPawn.h"


// Sets default values
AMyPawn::AMyPawn()
{
    // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;
    //设置为玩家控制
    AutoPossessPlayer = EAutoReceiveInput::Player0;

    //添加一个可以添加对象的空根组件
    RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
    //创建相机和可见项目
    UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
    OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent"));
    //附加相机和可见对象到根组建,偏移并旋转相机。
    OurCamera->AttachTo(RootComponent);
    OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
    OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
    OurVisibleComponent->AttachTo(RootComponent);

}

// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
    Super::BeginPlay();

}

// Called every frame
void AMyPawn::Tick( float DeltaTime )
{
    Super::Tick(DeltaTime);
    //基于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 NewLoaction = GetActorLocation() + (CurrentVelocity*DeltaTime);
            SetActorLocation(NewLoaction);
        }
    }
}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
    Super::SetupPlayerInputComponent(InputComponent);
    //响应Grow键
    InputComponent->BindAction("Grow",IE_Pressed,this,&AMyPawn::StartGrowing);
    InputComponent->BindAction("Grow",IE_Released,this,&AMyPawn::StopGrowing);

    //响应两个坐标轴值
    InputComponent->BindAxis("MoveX", this, &AMyPawn::Move_XAxis);
    InputComponent->BindAxis("MoveY", this, &AMyPawn::Move_YAxis);
}

void AMyPawn::Move_XAxis(float AxisValue)
{
    //前后
    CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f)*100.0f;
}

void AMyPawn::Move_YAxis(float AxisValue)
{
    //左右
    CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f)*100.0f;

}

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

void AMyPawn::StopGrowing()
{
    bGrowing = false;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值