UE C++ Pawn的旋转缩放(记录一下)

gamemode.h

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


#include "MyGameModeBase.h"
#include "MyPawn.h"
#include "MyPlayerController.h"

AMyGameModeBase::AMyGameModeBase()
{
	//AGameModeBase = AMyGameModeBase::StaticClass();
	PlayerControllerClass = AMyPlayerController::StaticClass();
	DefaultPawnClass = AMyPawn::StaticClass();
}

gamemode.cpp

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "MyGameModeBase.generated.h"

/**
 * 
 */
UCLASS()
class CPP511SELF_API AMyGameModeBase : public AGameModeBase
{
	GENERATED_BODY()
	
public:
	AMyGameModeBase();
};

 pawn.h

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

#pragma once

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

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

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

	UPROPERTY(VisibleAnywhere)
		class USpringArmComponent* SpringArm;
	    class UCameraComponent* camera;
		class APlayerController* playercontroller;

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:
	
	float armlength;

	void lookup(float value);
	void lookright(float value);
	void lookzoom(float zoom);



};

pawn.cpp 

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


#include "MyPawn.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "Kismet/GameplayStatics.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 = CreateAbstractDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
	SpringArm = CreateAbstractDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
	camera = CreateAbstractDefaultSubobject<UCameraComponent>(TEXT("camera"));

	SpringArm->SetupAttachment(GetRootComponent());
	camera->SetupAttachment(SpringArm);
	
	armlength = 500.0f;
	SpringArm->TargetArmLength = armlength;
	SpringArm->bDoCollisionTest = false;

	SpringArm->bUsePawnControlRotation = true;

	//playercontroller = UGameplayStatics::GetPlayerController(GetWorld(), 0);

}

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

	APlayerController* playercontrol = UGameplayStatics::GetPlayerController(GetWorld(), 0);
	playercontrol->bShowMouseCursor = true;

	
}

// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	PlayerInputComponent->BindAxis(TEXT("lookup"), this, &AMyPawn::lookup);
	PlayerInputComponent->BindAxis(TEXT("lookright"), this, &AMyPawn::lookright);
	PlayerInputComponent->BindAxis(TEXT("lookzoom"), this, &AMyPawn::lookzoom);

}

void AMyPawn::lookup(float value)
{
	APlayerController* playercontrol = UGameplayStatics::GetPlayerController(GetWorld(), 0);

	if (playercontrol->IsInputKeyDown(EKeys::LeftMouseButton))
	{
		AddControllerPitchInput(value);

	}
	
}

void AMyPawn::lookright(float value)
{
	APlayerController* playercontrol = UGameplayStatics::GetPlayerController(GetWorld(), 0);

	if (playercontrol->IsInputKeyDown(EKeys::LeftMouseButton))
	{
		AddControllerYawInput(value);

	}
	
}

void AMyPawn::lookzoom(float zoom)
{
	SpringArm->TargetArmLength = FMath::Clamp((SpringArm->TargetArmLength + 1 * zoom), 100.0f, 600.0f);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我救我自己

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值