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