【UE4 C++】实现旋转小球的第三人称自由视角

本文将介绍用C++实现一个简单的玩家可通过WASD控制移动,Shift进行加速,鼠标控制视角旋转和缩放的小球。

本人也只是一个UE4初学者,大佬勿喷。


一、技术难点

  • 小球通过角速度控制旋转,因此想实现自由视角相机,它就不能作为小球的子物体。
  • 小球的移动方向始终要保持与视野前方相同。

二、最终效果图

 

三、核心代码模块

在此先将模块分类,使结构清晰明了,最后有完整代码,可具体查看。


1、首先创建组件

  • SphereBase.h(自己创建的C++类)下
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "RootComp")
	    class USceneComponent * RootComp;//声明根节点组件

    UPROPERTY(EditAnywhere, BlueprintReadWrite,Category = "SphereMeshComp")
	    class UStaticMeshComponent * SphereMeshComp;//小球Mesh组件

    UPROPERTY(EditAnywhere, BlueprintReadWrite,Category = "CameraArmComp")
    	class USpringArmComponent * CameraArmComp;//相机臂组件

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CameraComp")
    	class UCameraComponent * CameraComp;//相机组件
  •  SphereBase.cpp
    //创建组件
    RootComp = CreateDefaultSubobject<USceneComponent>(TEXT("RootComp"));
    SphereMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SphereBaseComp"));
    CameraArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraArmComp"));
    CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));

    //添加组件父子关系
    SphereMeshComp->SetupAttachment(RootComp);
    CameraArmComp->SetupAttachment(RootComp);
    CameraComp->SetupAttachment(CameraArmComp);

    //设置小球物理效果为真
    SphereMeshComp->SetSimulatePhysics(true);
  • 创建蓝图类继承该C++类后,组件已创建 

2、绑定按键输入

 

    //绑定前后左右移动
    PlayerInputComponent->BindAxis("MoveForward", this, &ASphereBase::MoveForward);
    PlayerInputComponent->BindAxis("MoveRight", this, &ASphereBase::MoveRight);
    //Shift按下与擡起
    PlayerInputComponent->BindAction("MoveQuick", IE_Pressed, this, &ASphereBase::MoveQuick);
PlayerInputComponent->BindAction("MoveQuick", IE_Released, this, &ASphereBase::MoveNormal);
    //相机上下左右旋转
    PlayerInputComponent->BindAxis("CameraYaw", this, &ASphereBase::YawCamera);
    PlayerInputComponent->BindAxis("CameraPitch", this, &ASphereBase::PitchCamera);
    //相机缩放
    PlayerInputComponent->BindAction("ZoomIn", IE_Pressed, this, &ASphereBase::ZoomIn);
    PlayerInputComponent->BindAction("ZoomIn", IE_Released, this, &ASphereBase::ZoomStop);
    PlayerInputComponent->BindAction("ZoomOut", IE_Pressed, this, &ASphereBase::ZoomOut);
    PlayerInputComponent->BindAction("ZoomIn", IE_Released, this, &ASphereBase::ZoomStop);

具体函数请查看完整代码 

3、相机自由视角

建议采用世界座标系函数修改值,不要使用相对座标系Relative函数,否则会遇到很多问题。


  • 小球移动控制
    if (!AngularVector.IsZero())
    {
        FVector NewVector = FVector(0, 0, 0);
        NewVector += AngularVector.X  * CameraArmComp->GetForwardVector() * SphereSpeed;
        NewVector += AngularVector.Y  * CameraArmComp->GetRight	Vector() * SphereSpeed;
        SphereMeshComp->SetPhysicsAngularVelocity(NewVector);//给小球施加角速度向量
    }
  • 相机臂左右旋转
    FRotator LRRotation = CameraArmComp->GetComponentRotation();
    LRRotation.Yaw += CameraInput.X;
    CameraArmComp->SetWorldRotation(LRRotation);
  • 相机臂上下旋转 
    FRotator UDRotation = CameraArmComp->GetComponentRotation();
    UDRotation.Pitch = FMath::Clamp(UDRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);//控制上下视野范围
    CameraArmComp->SetWorldRotation(UDRotation);
  • 相机臂跟随小球
    FVector NewLocation = SphereMeshComp->GetComponentLocation();
    CameraArmComp->SetWorldLocation(NewLocation);

4、相机缩放 

    ZoomValue = FMath::Clamp<float>(ZoomValue, 0.0f, 1.0f);
    //基于ZoomFActor来混合控制相机的视域和弹簧臂的长度 0.0f对应90.0f 1500.0f
    CameraComp->FieldOfView = FMath::Lerp<float>(90.0f, 60.0f, ZoomValue);
    CameraArmComp->TargetArmLength = FMath::Lerp<float>(1500.0f, 500.0f, ZoomValue);

滚轮控制ZoomValue值的变化

四、完整代码

  •  Sphere.h
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "SphereBase.generated.h"//必须放在头文件最后

UCLASS()
class BILICODE_API ASphereBase : public APawn//APawn 继承 Actor
{
    GENERATED_BODY()

public:
    // Sets default values for this pawn's properties
    ASphereBase();

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "RootComp")
        class USceneComponent * RootComp;

    UPROPERTY(EditAnywhere, BlueprintReadWrite,Category = "SphereMeshComp")
        class UStaticMeshComponent * SphereMeshComp;//class声明

    UPROPERTY(EditAnywhere, BlueprintReadWrite,Category = "CameraArmComp")
        class USpringArmComponent * CameraArmComp;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CameraComp")
        class UCameraComponent * CameraComp;

public:
    FVector AngularVector;
    float SphereSpeed;
    float SpeedMin;
    float SpeedMax;

    FVector CameraInput;
    float ZoomValue;
    UPROPERTY(EditAnyWhere, BlueprintReadWrite)
        bool IsInput;//控制是否能输入按键

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;

    UFUNCTION(BlueprintCallable)
        void MoveForward(float AxisValue);
    UFUNCTION(BlueprintCallable)
        void MoveRight(float AxisValue);
    UFUNCTION(BlueprintCallable)
        void MoveQuick();
    UFUNCTION(BlueprintCallable)
        void MoveNormal();

    void PitchCamera(float AxisValue);
    void YawCamera(float AxisValue);

    void StartJump();
    void StopJump();

    void ZoomIn();
    void ZoomStop();
    void ZoomOut();
};
  • Sphere.cpp
// Fill out your copyright notice in the Description page of Project Settings.

#include "SphereBase.h"
#include "Components/StaticMeshComponent.h"//Mesh头文件
#include "GameFramework/SpringArmComponent.h"//摄像机手臂头文件
#include "Camera/CameraComponent.h"
#include "Components/SceneComponent.h"
#include "Components/InputComponent.h"//输入按键绑定头文件
#include "Engine.h"

// Sets default values
ASphereBase::ASphereBase()
{
    // 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;

    IsInput = true;
    SphereSpeed = 300.0f;
    SpeedMin = SphereSpeed;
    SpeedMax = 500.0f;
    ZoomValue = 0.5f;

    //创建组件
    RootComp = CreateDefaultSubobject<USceneComponent>(TEXT("RootComp"));
    SphereMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SphereBaseComp"));
    CameraArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraArmComp"));
    CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));

    //组件关系
    SphereMeshComp->SetupAttachment(RootComp);
    CameraArmComp->SetupAttachment(RootComp);
    CameraComp->SetupAttachment(CameraArmComp);
	
    //设置物理效果为真
    SphereMeshComp->SetSimulatePhysics(true);
}

// Called when the game starts or when spawned
void ASphereBase::BeginPlay()
{
    Super::BeginPlay();
}


// Called every frame
void ASphereBase::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    if (!AngularVector.IsZero())
    {
        FVector NewVector = FVector(0, 0, 0);
        NewVector += AngularVector.X  * CameraArmComp->GetForwardVector() * SphereSpeed;
        NewVector += AngularVector.Y  * CameraArmComp->GetRightVector() * SphereSpeed;
        SphereMeshComp->SetPhysicsAngularVelocity(NewVector);//小球向一个向量方向旋转移动
    }

    {//相机臂左右旋转(相机臂与小球是兄弟关系
        FRotator LRRotation = CameraArmComp->GetComponentRotation();
        LRRotation.Yaw += CameraInput.X;
        CameraArmComp->SetWorldRotation(LRRotation);
    }

    {//相机臂跟随小球
        FVector NewLocation = SphereMeshComp->GetComponentLocation();
        CameraArmComp->SetWorldLocation(NewLocation);
        //两种方便的调试方法
        //GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Purple, NewLocation.ToString());
        /*DrawDebugLine(
            GetWorld(),
            SphereBeginLocation,
            SphereMeshComp->GetComponentLocation(),
            FColor::Red,
            false, -1, 0,
            3.
        );*/
    }
    {//相机臂上下旋转
        FRotator UDRotation = CameraArmComp->GetComponentRotation();
        UDRotation.Pitch = FMath::Clamp(UDRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);//控制旋转范围
        CameraArmComp->SetWorldRotation(UDRotation);
    }

    {//相机缩放
        ZoomValue = FMath::Clamp<float>(ZoomValue, 0.0f, 1.0f);
        //基于ZoomFActor来混合相机的视域和弹簧臂的长度
        CameraComp->FieldOfView = FMath::Lerp<float>(90.0f, 60.0f, ZoomValue);
        CameraArmComp->TargetArmLength = FMath::Lerp<float>(1500.0f, 500.0f, ZoomValue);
    }
}

// Called to bind functionality to input
void ASphereBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)//pawn不同于actor的地方,用于绑定按键
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    PlayerInputComponent->BindAxis("MoveForward", this, &ASphereBase::MoveForward);//绑定 前后移动映射 的函数
    PlayerInputComponent->BindAxis("MoveRight", this, &ASphereBase::MoveRight);//绑定左右
    PlayerInputComponent->BindAction("MoveQuick", IE_Pressed, this, &ASphereBase::MoveQuick);
    PlayerInputComponent->BindAction("MoveQuick", IE_Released, this, &ASphereBase::MoveNormal);
    PlayerInputComponent->BindAxis("CameraYaw", this, &ASphereBase::YawCamera);
    PlayerInputComponent->BindAxis("CameraPitch", this, &ASphereBase::PitchCamera);
    PlayerInputComponent->BindAction("ZoomIn", IE_Pressed, this, &ASphereBase::ZoomIn);
    PlayerInputComponent->BindAction("ZoomIn", IE_Released, this, &ASphereBase::ZoomStop);
    PlayerInputComponent->BindAction("ZoomOut", IE_Pressed, this, &ASphereBase::ZoomOut);
    PlayerInputComponent->BindAction("ZoomIn", IE_Released, this, &ASphereBase::ZoomStop);
}

//前后左右输入控制
void ASphereBase::MoveForward(float AxisValue)
{
    if (IsInput)
    {
        AngularVector.Y = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
    }
}

void ASphereBase::MoveRight(float AxisValue)
{
    if (IsInput)
    {
        AngularVector.X = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
    }
}
//shift输入控制
void ASphereBase::MoveQuick()
{
    SphereSpeed = SpeedMax;
}

void ASphereBase::MoveNormal()
{
    SphereSpeed = SpeedMin;
}
//相机旋转输入控制
void ASphereBase::PitchCamera(float AxisValue)
{
    CameraInput.Y = AxisValue;
}

void ASphereBase::YawCamera(float AxisValue)
{
    CameraInput.X = AxisValue;
}

void ASphereBase::ZoomIn()
{
    ZoomValue += 0.1f;
}
//相机缩放输入控制
void ASphereBase::ZoomStop()
{
}

void ASphereBase::ZoomOut()
{
    ZoomValue -= 0.1f;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章