APawn類,新增軸對映,使能夠控制APawn移動
APawn類,新增軸對映,使能夠控制APawn移動
MyPawn.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Components/StaticMeshComponent.h"
#include "Camera/CameraComponent.h"
#include "MyPawn.generated.h"
UCLASS()
class FIRSTDEMO_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
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;
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* MyStaticMesh;
UPROPERTY(VisibleAnywhere)
UCameraComponent* MyCamera;
UPROPERTY(EditAnywhere)
float MaxSpeed;//新增pawn移動速度引數
private:
//新增軸對映函式,MoveForward,MoveRight,Velocity
//設為私有變數,Pawn類裡自身進行處理,無需暴露給外部
void MoveForward(float Value);
void MoveRight(float Value);
FVector Velocity;
};
MyPawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.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=CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
MyStaticMesh->SetupAttachment(GetRootComponent());
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
MyCamera->SetupAttachment(GetRootComponent());
MyCamera->SetRelativeLocation(FVector(-300.f, 0.f, 300.f));
MyCamera->SetRelativeRotation(FRotator(-45.f, 0.f, 0.f));
AutoPossessPlayer = EAutoReceiveInput::Player0;
//初始化
MaxSpeed = 100.f;
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);
//在tick中新增偏移函式
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)
{
//向前的速度,利用Clamp 將value限制在-1,1之間,放置Ue4編輯器中數值過大,產生bug
Velocity.X = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}
void AMyPawn::MoveRight(float Value)
{
//向右的速度,利用Clamp 將value限制在-1,1之間,放置Ue4編輯器中數值過大,產生bug
Velocity.Y = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}
相關文章
- MyBatis(四) 對映器配置(自動對映、resultMap手動對映、引數傳遞)MyBatis
- 使Snowflake的客戶能夠通過Snowflake平臺
- 如何在CSS中對映的滑鼠位置,並實現通過滑鼠移動控制頁面元素效果CSS
- 使你的 Python 遊戲玩家能夠向前和向後跑Python遊戲
- .NET CORE AUTOMAPPER 對映一個類的子類APP
- Yii2 在控制器內對映 Action 到其它動作
- linux mdev實現裝置符重對映/裝置符手動新增Linuxdev
- 相對定位指令驅動X軸
- ArgoDB 3.2新增智慧建表演算法,在建表時能夠自動設定Go演算法
- elasticsearch的字串動態對映Elasticsearch字串
- SpringBoot中能在控制器中對映dto嗎?Spring Boot
- flutter: CSS規則對映flutter控制元件-positionFlutterCSS控制元件
- 基於AbstractProcessor擴充套件MapStruct自動生成實體對映工具類套件Struct
- HTML+CSS實現時間軸(移動端)HTMLCSS
- 對映
- EntityFramework Core如何對映動態模型?Framework模型
- Qt動態新增控制元件QT控制元件
- 四軸機械臂控制機械臂
- 時間軸、流程類時間軸繪製
- Laravel-admin 表格新增捲軸Laravel
- ASP.NET Core-自動對映ASP.NET
- Docker 如何動態修改容器埠對映Docker
- WPF給控制元件新增運動動畫控制元件動畫
- mybatis 實體類排除資料庫欄位對映MyBatis資料庫
- SpringBoot JPA查詢對映到自定義實體類Spring Boot
- 多重對映
- 用 Pygame 使你的遊戲角色移動起來GAM遊戲
- Elasticsearch第四篇:索引別名、新增或修改對映規則Elasticsearch索引
- 【ASP.NET Core】動態對映MVC路由ASP.NETMVC路由
- NAS對映網路驅動器如何操作?
- MyBatis框架之SQL對映和動態SQLMyBatis框架SQL
- riok/mapperly: 類似MapStruct的.NET物件對映生成器APPStruct物件
- 直播帶貨原始碼,css隱藏捲軸,但使超出部分任然可以滾動原始碼CSS
- 如何使軟體標準真正能夠落地?且看《深入理解GJB5000A》公開課!
- [非專業翻譯] Mapster - 對映前&對映後
- 剪映app怎麼給影片新增字幕?剪映app給影片新增文字的方法教程APP
- 使用TypeDescriptor給類動態新增Attribute
- Cache對映方式