新建一个基于C++的工程MyProject,默认会创建游戏模式类AMyProjectGameModeBase,如果自己没有创建Pawn,DefaultPawnClass就会默认为ADefaultPawn类,打包后会出现一些问题。比如想按下鼠标左键沿X轴或者Y轴旋转,代码做了相应操作,结果却会不按下鼠标左键就会旋转,不是我们想要的。自己写一个Pawn好控制些。这里只做一些简单的操控,鼠标左键按下可以左右、上下翻转,鼠标滚轮滚动可以拉远拉近。
AManipulator.h
#pragma once
#include "GameFramework/Pawn.h"
#include "Manipulator.generated.h"
UCLASS()
class MYPROJECT_API AManipulator : public APawn
{
GENERATED_UCLASS_BODY()
public:
// Sets default values for this pawn's properties
AManipulator();
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;
virtual UPawnMovementComponent* GetMovementComponent() const override;
virtual void UpdateNavigationRelevance() override;
virtual void MouseX(float Val);
virtual void MouseY(float Val);
virtual void MouseScrollUp();
virtual void MouseScrollDown();
protected:
float BaseTurnRate;
float BaseSpeed;
UPawnMovementComponent* MovementComponent;
USphereComponent* CollisionComponent;
};