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();
}

// 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( 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(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

 

 

 

 

蓝图控制

UE4 控制人物的行动和旋转_第1张图片

你可能感兴趣的:(UE4)