Category
= “My Variables”),中的Category是分类的意思,可以在蓝图调用的时候去显现出来会将你的东西挂载到这个分类下面。// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "MyObject.generated.h"
/**
*
*/
UCLASS(Blueprintable)
class CPROJECT_API UMyObject : public UObject
{
GENERATED_BODY()
public:
//构造函数
UMyObject();
UPROPERTY(BlueprintReadWrite, Category = "My Variables")//声明变量可以蓝图系统中进行读写
//变量
float xiaogua;
UFUNCTION(BlueprintCallable, Category = "My Functions")//声明函数可以在蓝图中进行调用
//函数成员
void myFunction();
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyObject.h"
UMyObject::UMyObject()
{
xiaogua = 0.0f;
}
void UMyObject::myFunction()
{
//第一个变量是输出类型,第二个变量是输出级别,第三个输出内容
//LogTemp临时类型,Log,Warning,Error输出级别
UE_LOG(LogTemp,Log,TEXT("Hello,World"));
UE_LOG(LogTemp, Warning, TEXT("Hello,World"));
UE_LOG(LogTemp, Error, TEXT("Hello,World"));
}
A
前缀,如AControllerU
前缀,如UComponentE
,如EFortificationTypeI
,如IAbilitySystemInterfaceT
,如TArrayS
,如SButtonF
,如FVector// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class CPROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// Set this actor 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 AMyActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
UPROPERTY
(VisibleAnyWhere,Category = " MyStatic")
VisibleAnyWhere
:设置可见属性为全部可见Category
:种类 //设置组件属性与种类
UPROPERTY(VisibleAnyWhere, Category = "MyStatic")
//定义一个静态组件
UStaticMeshComponent* MyStatic;
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class CPROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
//设置组件属性与种类
UPROPERTY(VisibleAnyWhere, Category = "MyStatic")
//定义一个静态组件
UStaticMeshComponent* MyStatic;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//创建默认根组件,如果报错,因为设置了文本,报错会提示MyStatic错误
MyStatic = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStatic"));
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
UPROPERTY(EditInstanceOnly, Category = "MyActorProperties | Vector")
//新建一个三维空间向量变量
FVector InitLocation;
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class CPROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
//设置组件属性与种类
UPROPERTY(VisibleAnyWhere, Category = "MyStatic")
//定义一个静态组件
UStaticMeshComponent* MyStatic;
UPROPERTY(EditInstanceOnly, Category = "MyActorProperties | Vector")
//新建一个三维空间向量变量
FVector InitLocation;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//创建默认根组件,如果报错,因为设置了文本,报错会提示MyStatic错误
MyStatic = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStatic"));
//构造赋初值
InitLocation = FVector(0.0f);
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
SetActorLocation(InitLocation);
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
UPROPERTY(VisibleInstanceOnly, Category = "MyActorProperties | Vector")
//新建一个三维空间向量变量,用来记录上一次位置
FVector PlacedLocation;
EditDefaultsOnly
,Category = “MyActorProperties | Vector”)
UPROPERTY(EditDefaultsOnly,Category = "MyActorProperties | Vector")
//虚幻的bool变量前必须加上b,程序会默认去掉的,但是必须定义时必须要有
bool bGotoInitLocation;
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class CPROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
//设置组件属性与种类
UPROPERTY(VisibleAnyWhere, Category = "MyStatic")
//定义一个静态组件
UStaticMeshComponent* MyStatic;
UPROPERTY(EditInstanceOnly, Category = "MyActorProperties | Vector")
//新建一个三维空间向量变量
FVector InitLocation;
UPROPERTY(VisibleInstanceOnly,Category = "MyActorProperties | Vector")
//新建一个三维空间向量变量,用来记录上一次
FVector PlacedLocation;
UPROPERTY(EditDefaultsOnly,Category = "MyActorProperties | Vector")
//虚幻的bool变量前必须加上b,程序会默认去掉的,但是必须定义时必须要有
bool bGotoInitLocation;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//创建默认根组件,如果报错,因为设置了文本,报错会提示MyStatic错误
MyStatic = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStatic"));
//构造赋初值
InitLocation = FVector(0.0f);
PlacedLocation = FVector(0.0f);
bGotoInitLocation = false;
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
//获取位置
PlacedLocation = GetActorLocation();
if (bGotoInitLocation)
{
SetActorLocation(InitLocation);
}
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
VisibleDefaultsOnly
, Category= “MyActorProperties | Vector”)
EditAnywhere
, Category = “MyActorProperties | Vector”)
UPROPERTY(VisibleDefaultOnly,Category="MyActorProperties | Vector")
FVector WordOrigin;
UPROPERTY(EditAnywhere, Category = "MyActorProperties | Vector")
FVector LocationOffset;
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class CPROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
//设置组件属性与种类
UPROPERTY(VisibleAnyWhere, Category = "MyStatic")
//定义一个静态组件
UStaticMeshComponent* MyStatic;
UPROPERTY(EditInstanceOnly, Category = "MyActorProperties | Vector")
//新建一个三维空间向量变量
FVector InitLocation;
UPROPERTY(VisibleInstanceOnly,Category = "MyActorProperties | Vector")
//新建一个三维空间向量变量,用来记录上一次
FVector PlacedLocation;
UPROPERTY(EditDefaultsOnly,Category = "MyActorProperties | Vector")
//虚幻的bool变量前必须加上b,程序会默认去掉的,但是必须定义时必须要有
bool bGotoInitLocation;
UPROPERTY(VisibleDefaultsOnly, Category = "MyActorProperties | Vector")
FVector WordOrigin;
UPROPERTY(EditAnywhere, Category = "MyActorProperties | Vector")
FVector LocationOffset;
UPROPERTY(EditAnywhere, Category = "MyActorProperties | Vector")
FVector TickLocationOffset;
UPROPERTY(EditAnywhere, Category = "MyActorProperties | Vector")
bool bShouldMove;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//创建默认根组件,如果报错,因为设置了文本,报错会提示MyStatic错误
MyStatic = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStatic"));
//构造赋初值
InitLocation = FVector(0.0f);
PlacedLocation = FVector(0.0f);
bGotoInitLocation = false;
WordOrigin = FVector(0.0f);
TickLocationOffset = FVector(0.0f);
bShouldMove = false;
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
//获取位置
PlacedLocation = GetActorLocation();
if (bGotoInitLocation)
{
SetActorLocation(InitLocation);
}
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (bShouldMove)
{
AddActorLocalOffset(TickLocationOffset);
}
}
一般在组件的指针不会设置为EditAnywhere,因为这样会导致基于这个组件对象的所有派生的类都会被这个指针去指向,就会导致很多麻烦的问题
UPROPERTY(EditAnywhere
, Category = “MyActorProperties | Vector”, meta
= (ClampMin = -10, ClampMax = 10, UIMin = -10, UIMax = 10))
meta
=(ClampMin
= -10,ClampMax
= 10, UIMin
= -10, UIMax
= 10)):clamp:键盘输入值控制,ui:鼠标拉动值控制静态网格开启物理模拟,加上简单碰撞,为了方便测试,以下是开启物理模拟未开启重力
MyStatic->AddForce(InitForce, “NAME_None”, bIsForce);
MyStatic->AddTorque(InitTorque, “NAME_None”, bIsForce);
Torque
:Torque to apply. Direction is axis of rotation and magnitude is strength of torque.BoneName
: If a SkeletalMeshComponent, name of body to apply torque to. ‘None’ indicates root body.bAccelChange
:If true, Torque is taken as a change in angular acceleration instead of a physical torque (i.e. mass will have no effect).//添加增加力
MyStatic->AddForce(InitForce, "NAME_None", bIsForce);
//添加上力矩
MyStatic->AddTorque(InitTorque, "NAME_None", bIsForce);
//力
UPROPERTY(EditAnywhere, Category = "MyActorProperties | Physics")
FVector InitForce;
//力矩
UPROPERTY(EditAnywhere, Category = "MyActorProperties | Physics")
FVector InitTorque;
//是否开启力
UPROPERTY(EditAnywhere, Category = "MyActorProperties | Physics")
bool bIsForce;
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//创建默认根组件,如果报错,因为设置了文本,报错会提示MyStatic错误
MyStatic = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStatic"));
//构造赋初值
InitLocation = FVector(0.0f);
PlacedLocation = FVector(0.0f);
bGotoInitLocation = false;
WordOrigin = FVector(0.0f);
TickLocationOffset = FVector(0.0f);
bShouldMove = false;
InitForce = FVector(0.0f);
InitTorque = FVector(0.0f);
bIsForce = false;
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
//获取位置
PlacedLocation = GetActorLocation();
if (bGotoInitLocation)
{
SetActorLocation(InitLocation);
}
//添加增加力
MyStatic->AddForce(InitForce, "NAME_None", bIsForce);
//添加上力矩
MyStatic->AddTorque(InitTorque, "NAME_None", bIsForce);
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (bShouldMove)
{
AddActorLocalOffset(TickLocationOffset);
}
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (bShouldMove)
{
FHitResult HitReslut;
AddActorLocalOffset(TickLocationOffset, true, &HitReslut);
UE_LOG(LogTemp, Warning, TEXT("X %f,Y %f,Z %f", HitReslut.Location.X, HitReslut.Location.Y, HitReslut.Location.Z));
}
}
//此RootComponent是Pawn类声明好的,直接指定即可
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
//将根组件附加搭配静态网格上
MyStaticMesh->SetupAttachment(GetRootComponent());
// 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 CPROJECT_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
UPROPERTY(VisibleAnywhere, Category = "My Pawn Components")
UStaticMeshComponent* MyStaticMesh;
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;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.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是Pawn类声明好的,直接指定即可
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
//将根组件附加搭配静态网格上
MyStaticMesh->SetupAttachment(GetRootComponent());
}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
}
// 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);
}
#include "Camera/CameraComponent.h"
:Camera的头文件// 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 CPROJECT_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
UPROPERTY(VisibleAnywhere, Category = "My Pawn Components")
UStaticMeshComponent* MyStaticMesh;
UPROPERTY(VisibleAnywhere, Category = "My Pawn Components")
//用class进行一下标明,遵循UE语法
class UCameraComponent* MyCamera;
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;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "Camera/CameraComponent.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是Pawn类声明好的,直接指定即可
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
//将根组件附加搭配静态网格上
MyStaticMesh->SetupAttachment(GetRootComponent());
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
MyCamera->SetupAttachment(GetRootComponent());
//设置组件与根组件为相对位置
MyCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 300.0f));
MyCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 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);
}
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
//决定哪个PlayerController(如果有的话)应该在关卡开始或生成小兵时自动拥有该小兵
AutoPossessPlayer = EAutoReceiveInput::Player0;
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是Pawn类声明好的,直接指定即可
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
//将根组件附加搭配静态网格上
MyStaticMesh->SetupAttachment(GetRootComponent());
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
MyCamera->SetupAttachment(GetRootComponent());
//设置组件与根组件为相对位置
MyCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 300.0f));
MyCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
//决定哪个PlayerController(如果有的话)应该在关卡开始或生成小兵时自动拥有该小兵
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
然后去Pawn类中添加两个处理移动的函数进行绑定
头文件 #include “Components/InputComponent.h”
BindAxis
(TEXT(“MoveForward”),this,&MoveForward);BindAxis
:Binds a delegate function an Axis defined in the project settings.Returned reference is only guaranteed to be valid until another axis is bound.MyPawn.h中声明两个移动函数
private:
//创建两个移动函数,进行绑定
void MoveForward(float Value);
void MoveRight(float Value);
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "Camera/CameraComponent.h"
#include "Components/InputComponent.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是Pawn类声明好的,直接指定即可
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
//将根组件附加搭配静态网格上
MyStaticMesh->SetupAttachment(GetRootComponent());
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
MyCamera->SetupAttachment(GetRootComponent());
//设置组件与根组件为相对位置
MyCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 300.0f));
MyCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
//决定哪个PlayerController(如果有的话)应该在关卡开始或生成小兵时自动拥有该小兵
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
}
// 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("MoveForward"),this, &AMyPawn::MoveForward);
PlayerInputComponent->BindAxis(TEXT("MoveRight"),this,&AMyPawn::MoveRight);
}
void AMyPawn::MoveForward(float Value)
{
}
void AMyPawn::MoveRight(float Value)
{
}
// 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 CPROJECT_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
UPROPERTY(VisibleAnywhere, Category = "My Pawn Components")
UStaticMeshComponent* MyStaticMesh;
UPROPERTY(VisibleAnywhere, Category = "My Pawn Components")
//用class进行一下标明,遵循UE语法
class UCameraComponent* MyCamera;
UPROPERTY(EditAnywhere, Category = "My Pawn Movement")
float MaxSpeed;
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;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "Camera/CameraComponent.h"
#include "Components/InputComponent.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是Pawn类声明好的,直接指定即可
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
//将根组件附加搭配静态网格上
MyStaticMesh->SetupAttachment(GetRootComponent());
//RootComponent->SetMobility(EComponentMobility::Type::Movable);
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
MyCamera->SetupAttachment(GetRootComponent());
//设置组件与根组件为相对位置
MyCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 300.0f));
MyCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
//决定哪个PlayerController(如果有的话)应该在关卡开始或生成小兵时自动拥有该小兵
AutoPossessPlayer = EAutoReceiveInput::Player0;
MaxSpeed = 100.0f;
//等效于Velocity = FVector(0.0f,0.0f,0.0f);
Velocity = FVector::ZeroVector;
}
// 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);
}
// 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);
}
void AMyPawn::MoveForward(float Value)
{
//FMath::Clamp(x,min,max)进行夹值
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;
}
// 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 CPROJECT_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
//用class进行一下标明,遵循UE语法
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;
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;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "Camera/CameraComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/SpringArmComponent.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是Pawn类声明好的,直接指定即可
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
//将根组件附加搭配静态网格上
MyStaticMesh->SetupAttachment(GetRootComponent());
MySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArm"));
MySpringArm->SetupAttachment(MyStaticMesh);
MySpringArm->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));//设置相对旋转
MySpringArm->TargetArmLength = 400.0f;//无碰撞时弹簧臂的自然长度
MySpringArm->bEnableCameraLag = true;//If true, camera lags behind target position to smooth its movement
//如果bEnableCameraLag为真,则控制相机到达目标位置的速度。低值较慢(更多延迟),高值较快(更少延迟),而零是即时的(没有延迟)。
MySpringArm->CameraLagSpeed = 3.0f;
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
//将摄像机附着到MySpringArm上
MyCamera->SetupAttachment(MySpringArm);
//决定哪个PlayerController(如果有的话)应该在关卡开始或生成小兵时自动拥有该小兵
AutoPossessPlayer = EAutoReceiveInput::Player0;
MaxSpeed = 100.0f;
//等效于Velocity = FVector(0.0f,0.0f,0.0f);
Velocity = FVector::ZeroVector;
}
// 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);
}
// 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);
}
void AMyPawn::MoveForward(float Value)
{
//FMath::Clamp(x,min,max)进行夹值
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;
}
//添加默认材质与默认材质球
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshAsset(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));
static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialAsset(TEXT("Material'/Engine/EditorMaterials/PersonaFloorMat.PersonaFloorMat'"));
if (StaticMeshAsset.Succeeded() && MaterialAsset.Succeeded())//判断是否加载成功
{
//设置默认材质与材质球
MyStaticMesh->SetStaticMesh(StaticMeshAsset.Object);
MyStaticMesh->SetMaterial(0, MaterialAsset.Object);
MyStaticMesh->SetWorldScale3D(FVector(0.5f));//设置默认缩放
}
// 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 CPROJECT_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
//用class进行一下标明,遵循UE语法
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* GetStaticMeshComponent() { return MyStaticMesh; }
FORCEINLINE USpringArmComponent* GetSpringArmComponent() { 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;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "Camera/CameraComponent.h"
#include "Components/InputComponent.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是Pawn类声明好的,直接指定即可
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
MyStaticMesh->SetupAttachment(GetRootComponent());//将根组件附加搭配静态网格上
//添加默认材质与默认材质球
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshAsset(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));
static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialAsset(TEXT("Material'/Engine/EditorMaterials/PersonaFloorMat.PersonaFloorMat'"));
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(GetStaticMeshComponent());
MySpringArm->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));//设置相对旋转
MySpringArm->TargetArmLength = 400.0f;//无碰撞时弹簧臂的自然长度
MySpringArm->bEnableCameraLag = true;//If true, camera lags behind target position to smooth its movement
//如果bEnableCameraLag为真,则控制相机到达目标位置的速度。低值较慢(更多延迟),高值较快(更少延迟),而零是即时的(没有延迟)。
MySpringArm->CameraLagSpeed = 3.0f;
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
//将摄像机附着到MySpringArm上
MyCamera->SetupAttachment(GetSpringArmComponent());
//决定哪个PlayerController(如果有的话)应该在关卡开始或生成小兵时自动拥有该小兵
AutoPossessPlayer = EAutoReceiveInput::Player0;
MaxSpeed = 100.0f;
//等效于Velocity = FVector(0.0f,0.0f,0.0f);
Velocity = FVector::ZeroVector;
}
// 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);
}
// 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);
}
void AMyPawn::MoveForward(float Value)
{
//FMath::Clamp(x,min,max)进行夹值
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;
}
因为Sweep扫描碰撞只对根组件有效,所以我们现在需要把根组件设置为静态网格体
设置静态网格为根组件:直接将静态网格组件赋值为RootComponent即可
MyPawn.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 CPROJECT_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
//用class进行一下标明,遵循UE语法
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* GetStaticMeshComponent() { return MyStaticMesh; }
FORCEINLINE USpringArmComponent* GetSpringArmComponent() { 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;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "Camera/CameraComponent.h"
#include "Components/InputComponent.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;
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
RootComponent = MyStaticMesh;//将StaticMesh设置为根组件
MyStaticMesh->SetCollisionProfileName(TEXT("Pawn"));//设置静态网格默认碰撞为Pawn
//添加默认材质与默认材质球
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshAsset(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));
static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialAsset(TEXT("Material'/Engine/EditorMaterials/PersonaFloorMat.PersonaFloorMat'"));
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(GetStaticMeshComponent());
MySpringArm->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));//设置相对旋转
MySpringArm->TargetArmLength = 400.0f;//无碰撞时弹簧臂的自然长度
MySpringArm->bEnableCameraLag = true;//If true, camera lags behind target position to smooth its movement
//如果bEnableCameraLag为真,则控制相机到达目标位置的速度。低值较慢(更多延迟),高值较快(更少延迟),而零是即时的(没有延迟)。
MySpringArm->CameraLagSpeed = 3.0f;
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
//将摄像机附着到MySpringArm上
MyCamera->SetupAttachment(GetSpringArmComponent());
//决定哪个PlayerController(如果有的话)应该在关卡开始或生成小兵时自动拥有该小兵
AutoPossessPlayer = EAutoReceiveInput::Player0;
MaxSpeed = 100.0f;
//等效于Velocity = FVector(0.0f,0.0f,0.0f);
Velocity = FVector::ZeroVector;
}
// 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);
}
// 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);
}
void AMyPawn::MoveForward(float Value)
{
//FMath::Clamp(x,min,max)进行夹值
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;
}
//新建两个接受映射函数
void LookUP(float Value);
void LookRight(float Value);
FVector2D MouseInput;
-----------------------------------------------------------------------------
//然后进行绑定
PlayerInputComponent->BindAxis(TEXT("LookUp"), this, &AMyPawn::LookUP);
PlayerInputComponent->BindAxis(TEXT("LookRight"), this, &AMyPawn::LookRight);
-----------------------------------------------------------------------------
//映射函数处理
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);
}
-----------------------------------------------------------------------------
//在到tick里面进行设置旋转
// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
AddActorLocalOffset(Velocity * DeltaTime, true);
//在UE中里面也可以是X:Roll,Y:Pitch,Z:Yaw
FRotator NewSpringArmRotation = MySpringArm->GetComponentRotation();//获取当前SpringArm旋转
//设置抬头只能到80度,低头只能到0度
NewSpringArmRotation.Pitch = FMath::Clamp(NewSpringArmRotation.Pitch += MouseInput.Y, -80.0f, 0.0f);
//设置SpringArm旋转
MySpringArm->SetWorldRotation(NewSpringArmRotation);
}
bUseControllerRotationYaw
= true;开启Controller Yaw方位继承AddControllerYawInput(MouseInput.X)
; 添加鼠标传入的左右旋转值到Controller// 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 CPROJECT_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
//用class进行一下标明,遵循UE语法
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* GetStaticMeshComponent() { return MyStaticMesh; }
FORCEINLINE USpringArmComponent* GetSpringArmComponent() { 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;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "Camera/CameraComponent.h"
#include "Components/InputComponent.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;
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
RootComponent = MyStaticMesh;//将StaticMesh设置为根组件
MyStaticMesh->SetCollisionProfileName(TEXT("Pawn"));//设置静态网格默认碰撞为Pawn
//添加默认材质与默认材质球
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshAsset(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));
static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialAsset(TEXT("Material'/Engine/EditorMaterials/PersonaFloorMat.PersonaFloorMat'"));
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(GetStaticMeshComponent());
MySpringArm->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));//设置相对旋转
MySpringArm->TargetArmLength = 400.0f;//无碰撞时弹簧臂的自然长度
MySpringArm->bEnableCameraLag = true;//If true, camera lags behind target position to smooth its movement
//如果bEnableCameraLag为真,则控制相机到达目标位置的速度。低值较慢(更多延迟),高值较快(更少延迟),而零是即时的(没有延迟)。
MySpringArm->CameraLagSpeed = 3.0f;
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
//将摄像机附着到MySpringArm上
MyCamera->SetupAttachment(GetSpringArmComponent());
//决定哪个PlayerController(如果有的话)应该在关卡开始或生成小兵时自动拥有该小兵
AutoPossessPlayer = EAutoReceiveInput::Player0;
//开启Controller继承
bUseControllerRotationYaw = true;
//移动速度
MaxSpeed = 100.0f;
//等效于Velocity = FVector(0.0f,0.0f,0.0f);
Velocity = FVector::ZeroVector;
}
// 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);
//添加鼠标传入的左右旋转值到Controller
AddControllerYawInput(MouseInput.X);
//在UE中里面也可以是X:Roll,Y:Pitch,Z:Yaw
FRotator NewSpringArmRotation = MySpringArm->GetComponentRotation();//获取当前SpringArm旋转
//设置抬头只能到80度,低头只能到0度
NewSpringArmRotation.Pitch = FMath::Clamp(NewSpringArmRotation.Pitch += MouseInput.Y, -80.0f, 0.0f);
//设置SpringArm旋转
MySpringArm->SetWorldRotation(NewSpringArmRotation);
}
// 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)
{
//FMath::Clamp(x,min,max)进行夹值
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;
}
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);
}