1. 程式人生 > >UE4 控制人物的行動和旋轉

UE4 控制人物的行動和旋轉

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "GameFramework/SpringArmComponent.h"
#include "MyCharacter.generated.h"

UCLASS()
class PERSONMOVE_API AMyCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AMyCharacter();

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;


	void MoveForward(float amount);
	void MoveRight(float amount);
	
	
    void Yaw(float amount); //繞豎軸y旋轉
	void Pitch(float amount);  //繞橫向軸x旋轉

private:
	USpringArmComponent* SpringArmComponent;

};
//MyCharacter.Cpp
// Fill out your copyright notice in the Description page of Project Settings.

#include "MyCharacter.h"
#include "Camera/CameraComponent.h"

// Sets default values
AMyCharacter::AMyCharacter()
{
 	// Set this character 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 AMyCharacter::BeginPlay()
{
	Super::BeginPlay();
	
	SpringArmComponent = FindComponentByClass<USpringArmComponent>();
}

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


}

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

	check(InputComponent); //檢查空指標

	//BindAxis第一個引數要在PlayerSetting的Input裡面設定
	InputComponent->BindAxis("Forward", this, &AMyCharacter::MoveForward);
	InputComponent->BindAxis("Right", this, &AMyCharacter::MoveRight);

	InputComponent->BindAxis("MouseXInput", this, &AMyCharacter::Yaw);
	InputComponent->BindAxis("MouseYInput", this, &AMyCharacter::Pitch);

}

void AMyCharacter::MoveForward(float amount)
{
	if (Controller && amount)
	{
		// GetActorForwardVector()取得角色向前的向量
		FVector fwd = GetActorForwardVector();
		// 我們呼叫AddMovementInput來在‘fwd’向量的方向上移動角色‘amount’個單位
		AddMovementInput(fwd, amount);
	}
}

void AMyCharacter::MoveRight(float amount)
{
	if (Controller && amount)
	{
		FVector left = GetActorRightVector();
		AddMovementInput(left, amount);
	}
}

//左右
void AMyCharacter::Yaw(float amount)
{
	//UE_LOG(LogTemp, Warning, TEXT("%f"),amount);
	if (Controller && amount)
	{
		// AddControllerYawInput()函式用於改變控制器的Yaw變數,即增加縱向軸旋轉量。
		// GetWorld()函式取得世界指標UWorld*,通過世界指標呼叫GetDeltaSeconds()取得每幀耗費的時間。
		// 之所以要乘以每幀耗費的時間,是為了使得每一【秒】都增加200.0f * amount的改變數。
		// 如果不乘以每幀耗費的時間,那麼每一【幀】都會增加200.0f * amount的改變數。(注意由於每秒渲染量不同,所以每秒的幀數不一定是固定的。)
		// 通過幀數來控制變數,那麼遊戲看起來就不那麼流暢。試想,機子效能好的時候遊戲角色動作就迅速,機子效能差的時候遊戲角色動作就慢,這對於玩家公平嗎?
		AddControllerYawInput(100.f * amount * GetWorld()->GetDeltaSeconds());
	//	UE_LOG(LogTemp, Warning, TEXT("yaw"));
	}
}
//上下
void AMyCharacter::Pitch(float amount)
{
	if (Controller && amount)
	{
		
		//這裡要在藍圖的Pawn設定
	//AddControllerPitchInput(20.f * amount * GetWorld()->GetDeltaSeconds());

	//float p = GetActorRotation().Pitch;
	//p=FMath::Clamp<float>( p, -12.0f, 23.0f);

	//FRotator fv = FRotator(p, GetActorRotation().Yaw, GetActorRotation().Roll);
	//SetActorRotation(fv);

		//控制搖臂
	 if (SpringArmComponent)
	 {
		 FRotator CurrentRotator = SpringArmComponent->GetForwardVector().Rotation();
		 float ChangeValue= 200.f * amount * GetWorld()->GetDeltaSeconds();
		 float ChangePitch = FMath::Clamp<float>(CurrentRotator.Pitch + ChangeValue, -80, 15);

		 FRotator NewRotator = FRotator(ChangePitch,0,0);
		 SpringArmComponent->SetRelativeRotation(NewRotator);
	
		 //獲得四元數
		// UE_LOG(LogTemp, Warning, TEXT("%s"),* SpringArmComponent->GetRelativeTransform().GetRotation().ToString());
		 //角度
		 UE_LOG(LogTemp, Warning, TEXT("%s"), *SpringArmComponent->GetForwardVector().Rotation().ToString());

	 }

	/*APlayerCameraManager* CurrentCamera = GetWorld()->GetFirstPlayerController()->PlayerCameraManager;
	UE_LOG(LogTemp, Warning, TEXT("%s"),*CurrentCamera->GetName());
	FRotator Fr= CurrentCamera->GetCameraRotation();
	Fr.Yaw += 200.f * amount * GetWorld()->GetDeltaSeconds();
	CurrentCamera->SetActorRelativeRotation(Fr);*/

	}
}

1建立一個C++類繼承於Character,即MyCharacter類,程式碼如上

2基於MyCharacter類建立一個藍圖

3設定GameMode