UE4 C++ Pawn常用的摇头操作

UE4 C++ 续简单移动 常用的左右摇头操作

上下左右移动

.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 BASICTRAINING_API AMyPawn : public APawn
{
	GENERATED_BODY()

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

	UPROPERTY(VisibleAnywhere, Category = "My Pawn Components")
	class UStaticMeshComponent* MyStaticMesh;

	UPROPERTY(VisibleAnywhere, Category = "My Pawn Components")
    class UCameraComponent* MyCamera;

	UPROPERTY(VisibleAnywhere, Category = "My Pawn Components")
	class USpringArmComponent* MySpringArm;

	UPROPERTY(EditAnywhere, Category = "My Pawn Movement")
	float MaxSpeed;

    FORCEINLINE	UStaticMeshComponent* GSMC() { return MyStaticMesh; }
	FORCEINLINE USpringArmComponent* GSAC() { return MySpringArm; }

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 MoveForward(float value);
	void MoveRight(float value);
	FVector Velocity;

	void LookUp(float value);
	void LookRight(float value);
	FVector2D MouseInput;//因为xy只有两个所以用这个可以节约一点点性能
};

.cpp文件

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


#include "MyPawn.h"
#include "Components/StaticMeshComponent.h"
#include "Components/InputComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "UObject/ConstructorHelpers.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;
	//RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
	//MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
	
	//MyStaticMesh->SetupAttachment(GetRootComponent());//附加到组件

	MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
	RootComponent = MyStaticMesh;//设置它为根组件
	MyStaticMesh->SetCollisionProfileName(TEXT("Pawn"));//设置碰撞的碰撞预设为Pwan

	static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshAsset(TEXT("StaticMesh'/Engine/BasicShapes/Cone.Cone'"));
	static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialAsset(TEXT("Material'/Engine/EditorMaterials/PersonaSky.PersonaSky'"));
	if (StaticMeshAsset.Succeeded() &&MaterialAsset.Succeeded())
	{
		MyStaticMesh->SetStaticMesh(StaticMeshAsset.Object);
		MyStaticMesh->SetMaterial(0, MaterialAsset.Object);
		MyStaticMesh->SetWorldScale3D(FVector(0.5f));
	}





	MySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArm"));
	MySpringArm->SetupAttachment(GSMC());
	MySpringArm->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
	MySpringArm->TargetArmLength = 400.0f;//臂长
	MySpringArm->bEnableCameraLag = true;//平滑跟随的效果
	MySpringArm->CameraLagSpeed = 3.0f;//平滑的速度


	MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
	
	MyCamera->SetupAttachment(GSAC());
	//MyCamera->SetupAttachment(GetRootComponent());
	//MyCamera->SetRelativeLocation(FVector(-300.0f,0.0f,300.0f));
	//MyCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));

	AutoPossessPlayer = EAutoReceiveInput::Player0;//设置poss为玩家0

	bUseControllerRotationYaw = true;//开启蓝图里面的Pwan的使用控制器旋转Yaw

	MaxSpeed = 100.0f;
	Velocity = FVector::ZeroVector;//和Fvector(0.0f)一样	

}

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

// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	AddActorLocalOffset(Velocity * DeltaTime, true);//这个的蓝图叫 设置Actor本地偏移

	FRotator NewSpringArmRotation = MySpringArm->GetComponentRotation();
	NewSpringArmRotation.Pitch = FMath::Clamp(NewSpringArmRotation.Pitch += MouseInput.Y, -80.0f, 0.0f);
	MySpringArm->SetWorldRotation(NewSpringArmRotation);

	AddControllerYawInput(MouseInput.X);//比较常用的左右摇头的方式
	

}

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

	PlayerInputComponent->BindAxis(TEXT("MoveForward"),this,&AMyPawn::MoveForward);
	PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &AMyPawn::MoveRight);

	PlayerInputComponent->BindAxis(TEXT("LookUp"), this, &AMyPawn::LookUp);
	PlayerInputComponent->BindAxis(TEXT("LookRight"), this, &AMyPawn::LookRight);

}

void AMyPawn::MoveForward(float value)
{
	Velocity.X = FMath::Clamp(value,-1.0f,1.0f) * MaxSpeed;
	
}

void AMyPawn::MoveRight(float value)
{
	Velocity.Y = FMath::Clamp(value, -1.0f, 1.0f) * MaxSpeed;
	UE_LOG(LogTemp, Log, TEXT("Velocity::%f"), Velocity.Y);
}

void AMyPawn::LookUp(float value)
{
	MouseInput.Y = FMath::Clamp(value, -1.0f, 1.0f);
}

void AMyPawn::LookRight(float value)
{
	MouseInput.X = FMath::Clamp(value, -1.0f, 1.0f);
}


在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Li~蒙一

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

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

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

打赏作者

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

抵扣说明:

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

余额充值