U4初步使用整理(四)Pawn简单操作

Pawn继承与aactor,能通过玩家输入和ai驱动。

AutoPossessPlayer = EAutoReceiveInput::Player0;设置吸收输入的pawn,添加多个此类pawn,Player0取得是最新添加到场景中的pawn.
UPROPERTY属性可以让代码中变量在ed中可见,同时不会每次跑场景都重新设置变量,赋予EditAnywhere属性可以在编辑器中赋值。

一个控制pawn移动和形变的例子。

    UCLASS()
    class BASICPROJ_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;


    private:
        void MoveX(float delta);
        void MoveY(float delta);
        void StartGrow();
        void StopGrow();

        FVector CurrentVelocity;
        bool bGrowing;

    public:
        UPROPERTY(EditAnywhere)
                USceneComponent *OurVisibleComponent;
            };
    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 );

float CurrentScale = OurVisibleComponent->GetComponentScale().X;
if (bGrowing)
{
    CurrentScale += DeltaTime;
}
else
{
    CurrentScale -= (DeltaTime * 0.5f);
}

CurrentScale = FMath::Clamp(CurrentScale, 1.f, 2.f);
OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));

if (!CurrentVelocity.IsZero())
{
    FVector newLocation = GetActorLocation() + CurrentVelocity * DeltaTime;
    SetActorLocation(newLocation);
}

    }

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

        InputComponent->BindAxis("Forward", this, &AMyPawn::MoveX);
        InputComponent->BindAxis("back", this, &AMyPawn::MoveY);

    }

    void AMyPawn::MoveX(float delta)
    {
        CurrentVelocity.X = FMath::Clamp(delta, -1.f, 1.f) * 100;
    }

    void AMyPawn::MoveY(float delta)
    {
        CurrentVelocity.Y = FMath::Clamp(delta, -1.f, 1.f) * 100;
    }

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

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

你可能感兴趣的:(U4初步使用整理(四)Pawn简单操作)