前面的文章咱们可以在场景中移动Actor,但是无法移动观察Actor的角度和方位,现在我们要设置可以移动摄像头的方位和观察的角度。
- 首先我们在【项目设置】->【输入】添加两个鼠标输入:MousePitch和MouseYaw
在代码中,绑定这两个输入内容。
PlayerInputComponent->BindAxis(TEXT("CameraPitch"), this, &AColliderPawn::cameraPitch);PlayerInputComponent->BindAxis(TEXT("CameraYaw"), this, &AColliderPawn::cameraYaw);
记录鼠标移动的数据
void AColliderPawn::cameraPitch(float value) {//UE_LOG(LogTemp, Warning, TEXT("cameraPitch: %f"), value);cameraInput.Y = value;
}void AColliderPawn::cameraYaw(float value) {//UE_LOG(LogTemp, Warning, TEXT("cameraYaw: %f"), value);cameraInput.X = value;
}
然后再Tick函数中实现功能
void AColliderPawn::Tick(float DeltaTime) {Super::Tick(DeltaTime);//左右移动FRotator newRotation = GetActorRotation();newRotation.Yaw += cameraInput.X;SetActorRotation(newRotation);//通过摇臂移动方位FRotator armRotation = mySpringArm->GetComponentRotation();//armRotation.Pitch += cameraInput.Y;armRotation.Pitch = FMath::Clamp((double)(armRotation.Pitch += cameraInput.Y), (double)-80.0, double(-15.0));mySpringArm->SetWorldRotation(armRotation);
}
完整的代码:
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "ColliderPawn.generated.h"UCLASS()
class FIRSTPROJECT_API AColliderPawn : public APawn
{GENERATED_BODY()public:// Sets default values for this pawn's propertiesAColliderPawn();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public: // Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;public://静态网格体UPROPERTY(VisibleAnywhere, Category = "MyMess")UStaticMeshComponent* myMess;FORCEINLINE void setMessComponent(UStaticMeshComponent* mess) {myMess = mess;}FORCEINLINE UStaticMeshComponent* getMessComponent() {return myMess;}//包围球UPROPERTY(VisibleAnywhere, Category = "MyMess")class USphereComponent* mySphereComponent;FORCEINLINE void setSphereComponent(USphereComponent* sphere) {mySphereComponent = sphere;}FORCEINLINE USphereComponent* getSphereComponent() {return mySphereComponent;}//摄像机UPROPERTY(VisibleAnywhere, Category = "MyMess")class UCameraComponent* myCamera;FORCEINLINE void setCameraComponent(UCameraComponent* camera) {myCamera = camera;}FORCEINLINE UCameraComponent* getCameraComponent() {return myCamera;}//摇臂UPROPERTY(VisibleAnywhere, Category = "MyMess")class USpringArmComponent* mySpringArm;FORCEINLINE void setSpringArmComponent(USpringArmComponent* arm) {mySpringArm = arm;}FORCEINLINE USpringArmComponent* getSpringArmComponent() {return mySpringArm;}//移动组件UPROPERTY(VisibleAnywhere, Category = "MyMess")class UColliderMovementComponent* myMovementComponent;virtual UPawnMovementComponent* GetMovementComponent()const override;private:void moveForward(float value);void moveRight(float value);void cameraPitch(float value);void cameraYaw(float value);private:FVector2D cameraInput;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "ColliderPawn.h"
#include "Components/SphereComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "ColliderMovementComponent.h"// Sets default values
AColliderPawn::AColliderPawn() {// 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("Root"));mySphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("MySphere"));mySphereComponent->InitSphereRadius(40.0);mySphereComponent->SetCollisionProfileName(TEXT("Pawn"));mySphereComponent->SetupAttachment(GetRootComponent());myMess = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMess"));myMess->SetupAttachment(GetRootComponent());//通过代码的方式设置静态网格体static ConstructorHelpers::FObjectFinder<UStaticMesh> messAsset(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'"));if (messAsset.Succeeded()){myMess->SetStaticMesh(messAsset.Object);myMess->SetRelativeLocation(FVector(0.0, 0.0, -40.0));myMess->SetWorldScale3D(FVector(0.8));}mySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArm"));mySpringArm->SetupAttachment(GetRootComponent());mySpringArm->SetRelativeRotation(FRotator(-45.0, 0.0, 0.0));mySpringArm->TargetArmLength = 400.0;mySpringArm->bEnableCameraLag = true;mySpringArm->CameraLagSpeed = 3.0;myCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));myCamera->SetupAttachment(mySpringArm, USpringArmComponent::SocketName);myMovementComponent = CreateDefaultSubobject<UColliderMovementComponent>(TEXT("MyMovementCompoment"));myMovementComponent->UpdatedComponent = RootComponent;cameraInput = FVector2D(0.0);//自动支配AutoPossessPlayer = EAutoReceiveInput::Player0;
}// Called when the game starts or when spawned
void AColliderPawn::BeginPlay() {Super::BeginPlay();
}// Called every frame
void AColliderPawn::Tick(float DeltaTime) {Super::Tick(DeltaTime);//左右移动FRotator newRotation = GetActorRotation();newRotation.Yaw += cameraInput.X;SetActorRotation(newRotation);//通过摇臂移动方位FRotator armRotation = mySpringArm->GetComponentRotation();//armRotation.Pitch += cameraInput.Y;armRotation.Pitch = FMath::Clamp((double)(armRotation.Pitch += cameraInput.Y), (double)-80.0, double(-15.0));mySpringArm->SetWorldRotation(armRotation);
}// Called to bind functionality to input
void AColliderPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {Super::SetupPlayerInputComponent(PlayerInputComponent);PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &AColliderPawn::moveForward);PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &AColliderPawn::moveRight);PlayerInputComponent->BindAxis(TEXT("CameraPitch"), this, &AColliderPawn::cameraPitch);PlayerInputComponent->BindAxis(TEXT("CameraYaw"), this, &AColliderPawn::cameraYaw);
}UPawnMovementComponent* AColliderPawn::GetMovementComponent()const {return myMovementComponent;
}void AColliderPawn::moveForward(float value) {FVector forword = GetActorForwardVector();if (myMovementComponent){myMovementComponent->AddInputVector(forword * value);}
}void AColliderPawn::moveRight(float value) {FVector right = GetActorRightVector();if (myMovementComponent) {myMovementComponent->AddInputVector(right * value);}
}void AColliderPawn::cameraPitch(float value) {//UE_LOG(LogTemp, Warning, TEXT("cameraPitch: %f"), value);cameraInput.Y = value;
}void AColliderPawn::cameraYaw(float value) {//UE_LOG(LogTemp, Warning, TEXT("cameraYaw: %f"), value);cameraInput.X = value;
}
aaa