UE4 通过按键控制Pawn比例大小

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "GrowPawn.generated.h"

UCLASS()
class TRUEFPSPROJECT_API AGrowPawn : public APawn {
    
    
	GENERATED_BODY()

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

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

public:
	// Called every frame
	virtual void Tick(float DeltaTime) override;

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

private:
	void growScale();

private:
	bool isGrow = false;
};

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


#include "GrowPawn.h"
#include "Math/UnrealMathUtility.h"

// Sets default values
AGrowPawn::AGrowPawn() {
    
    
	// 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;

}

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

}

// Called every frame
void AGrowPawn::Tick(float DeltaTime) {
    
    
	Super::Tick(DeltaTime);
	float scale = RootComponent->GetComponentScale().X;
	if (isGrow) {
    
    //变大
		scale += DeltaTime;
	} else {
    
    
		scale -= DeltaTime;
	}

	scale = FMath::Clamp(scale, 1.0f, 2.0f);
	RootComponent->SetWorldScale3D(FVector(scale));
}

// Called to bind functionality to input
void AGrowPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
    
    
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAction("GrowScale", IE_Pressed, this, &AGrowPawn::growScale);
}

void AGrowPawn::growScale() {
    
    
	isGrow = !isGrow;
}


aaa

猜你喜欢

转载自blog.csdn.net/wb175208/article/details/128052085