虚拟摄像机在不同游戏类型中的应用
在上一节中,我们详细探讨了虚拟摄像机的基本概念和实现方法。本节将重点介绍虚拟摄像机在不同类型的动作游戏中的应用,包括第三人称动作游戏、第一人称射击游戏、平台跳跃游戏和开放世界游戏。每种游戏类型对虚拟摄像机的需求和实现方式都有所不同,我们将通过具体的案例和代码示例来展示如何在这些游戏中设计和实现虚拟摄像机。
第三人称动作游戏
第三人称动作游戏(如《鬼泣》、《忍者龙剑传》等)通常需要一个能够跟随角色的摄像机,同时提供多种视角切换的功能,以便玩家能够更好地观察和控制角色的动作。这种摄像机通常位于角色的后方,可以自由调整视角,以适应不同的战斗场景和动作需求。
摄像机跟随角色
在第三人称动作游戏中,摄像机需要跟随角色的移动。这可以通过在角色的蓝图中添加一个摄像机组件来实现。以下是一个简单的蓝图示例,展示了如何设置一个跟随角色的摄像机:
// 在角色蓝图中添加摄像机组件
// 1. 打开角色蓝图编辑器
// 2. 在组件列表中添加一个Camera组件
// 3. 将Camera组件的位置设置在角色后方,例如X=0, Y=-300, Z=150
// 4. 将Camera组件的旋转设置为俯视角度,例如Pitch=-30, Yaw=0, Roll=0
// 代码示例:在角色的C++类中设置摄像机
// MyCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"
UCLASS()
class MYGAME_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
// 构造函数
AMyCharacter();
protected:
// 虚拟摄像机组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera")
UCameraComponent* FollowCamera;
// 角色移动时调整摄像机位置
virtual void MoveForward(float Value) override;
virtual void MoveRight(float Value) override;
};
// MyCharacter.cpp
#include "MyCharacter.h"
#include "Camera/CameraComponent.h"
AMyCharacter::AMyCharacter()
{
// 设置默认摄像机位置
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(RootComponent);
FollowCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 150.0f));
FollowCamera->SetRelativeRotation(FRotator(-30.0f, 0.0f, 0.0f));
}
void AMyCharacter::MoveForward(float Value)
{
if (Value != 0.0f)
{
// 获取角色的前进方向
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// 转换为世界空间方向
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, Value);
// 调整摄像机位置
FollowCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 150.0f));
}
}
void AMyCharacter::MoveRight(float Value)
{
if (Value != 0.0f)
{
// 获取角色的右侧方向
const FRotator Rotation = Controller->GetControlRotation();
const FVector Direction = FRotationMatrix(Rotation).GetUnitAxis(EAxis::Y);
AddMovementInput(Direction, Value);
// 调整摄像机位置
FollowCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 150.0f));
}
}
摄像机视角切换
为了增强游戏的可玩性和视觉效果,第三人称动作游戏通常需要提供多种视角切换功能。例如,玩家可以在近距离战斗时切换到特写视角,以更好地观察角色的动作细节。以下是一个蓝图示例,展示了如何实现摄像机视角切换:
// 在角色蓝图中添加输入动作绑定
// 1. 打开角色蓝图编辑器
// 2. 进入Event Graph
// 3. 添加一个Input Action节点,例如命名为"ToggleCloseUp"
// 4. 将"ToggleCloseUp"节点连接到一个自定义函数
// 代码示例:在角色的C++类中实现视角切换
// MyCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"
UCLASS()
class MYGAME_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
AMyCharacter();
UFUNCTION(BlueprintCallable, Category = "Camera")
void ToggleCloseUpCamera();
protected:
UCameraComponent* FollowCamera;
UCameraComponent* CloseUpCamera;
bool bIsCloseUp;
};
// MyCharacter.cpp
#include "MyCharacter.h"
#include "Camera/CameraComponent.h"
AMyCharacter::AMyCharacter()
{
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(RootComponent);
FollowCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 150.0f));
FollowCamera->SetRelativeRotation(FRotator(-30.0f, 0.0f, 0.0f));
CloseUpCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("CloseUpCamera"));
CloseUpCamera->SetupAttachment(RootComponent);
CloseUpCamera->SetRelativeLocation(FVector(-100.0f, 0.0f, 100.0f));
CloseUpCamera->SetRelativeRotation(FRotator(-15.0f, 0.0f, 0.0f));
bIsCloseUp = false;
}
void AMyCharacter::ToggleCloseUpCamera()
{
if (bIsCloseUp)
{
// 切换回默认视角
FollowCamera->Activate();
CloseUpCamera->Deactivate();
bIsCloseUp = false;
}
else
{
// 切换到特写视角
CloseUpCamera->Activate();
FollowCamera->Deactivate();
bIsCloseUp = true;
}
}
第一人称射击游戏
第一人称射击游戏(如《使命召唤》、《战地》等)通常需要一个位于角色头部的摄像机,以提供真实的第一人称视角。这种摄像机需要能够跟随角色的头部动作,并且在瞄准时能够平滑地过渡到瞄准视角。
摄像机跟随角色头部
在第一人称射击游戏中,摄像机需要位于角色的头部,并且跟随角色的头部动作。这可以通过在角色的蓝图中添加一个摄像机组件并设置其位置来实现。以下是一个简单的蓝图示例,展示了如何设置一个跟随角色头部的摄像机:
// 在角色蓝图中添加摄像机组件
// 1. 打开角色蓝图编辑器
// 2. 在组件列表中添加一个Camera组件
// 3. 将Camera组件的位置设置在角色头部,例如X=0, Y=0, Z=100
// 4. 将Camera组件的旋转设置为默认角度,例如Pitch=0, Yaw=0, Roll=0
// 代码示例:在角色的C++类中设置摄像机
// MyCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"
UCLASS()
class MYGAME_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
AMyCharacter();
protected:
UCameraComponent* FirstPersonCamera;
};
// MyCharacter.cpp
#include "MyCharacter.h"
#include "Camera/CameraComponent.h"
AMyCharacter::AMyCharacter()
{
// 设置默认摄像机位置
FirstPersonCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
FirstPersonCamera->SetupAttachment(RootComponent);
FirstPersonCamera->SetRelativeLocation(FVector(0.0f, 0.0f, 100.0f));
FirstPersonCamera->SetRelativeRotation(FRotator(0.0f, 0.0f, 0.0f));
// 设置默认摄像机
CameraComponent = FirstPersonCamera;
}
摄像机瞄准平滑过渡
在第一人称射击游戏中,当玩家瞄准时,摄像机需要平滑地过渡到瞄准视角。这可以通过在角色的蓝图中添加动画曲线和摄像机平滑过渡逻辑来实现。以下是一个蓝图示例,展示了如何实现摄像机瞄准平滑过渡:
// 在角色蓝图中添加动画曲线
// 1. 打开角色蓝图编辑器
// 2. 在变量列表中添加一个Curve Float变量,例如命名为"AimBlendCurve"
// 3. 在Event Graph中添加一个Timeline节点,使用"AimBlendCurve"变量
// 4. 将Timeline节点的输出连接到摄像机的FOV和位置调整
// 代码示例:在角色的C++类中实现摄像机瞄准平滑过渡
// MyCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Curves/CurveFloat.h"
#include "MyCharacter.generated.h"
UCLASS()
class MYGAME_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
AMyCharacter();
UFUNCTION(BlueprintCallable, Category = "Camera")
void StartAiming();
UFUNCTION(BlueprintCallable, Category = "Camera")
void StopAiming();
protected:
UCameraComponent* FirstPersonCamera;
UCurveFloat* AimBlendCurve;
UTimelineComponent* AimTimeline;
float DefaultFOV;
float AimingFOV;
float CurrentFOV;
void OnAimProgress(float Progress);
void OnAimFinished();
};
// MyCharacter.cpp
#include "MyCharacter.h"
#include "Camera/CameraComponent.h"
#include "Curves/CurveFloat.h"
#include "Components/TimelineComponent.h"
AMyCharacter::AMyCharacter()
{
FirstPersonCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
FirstPersonCamera->SetupAttachment(RootComponent);
FirstPersonCamera->SetRelativeLocation(FVector(0.0f, 0.0f, 100.0f));
FirstPersonCamera->SetRelativeRotation(FRotator(0.0f, 0.0f, 0.0f));
DefaultFOV = 90.0f;
AimingFOV = 60.0f;
CurrentFOV = DefaultFOV;
FirstPersonCamera->SetFieldOfView(CurrentFOV);
AimBlendCurve = NewObject<UCurveFloat>();
AimTimeline = NewObject<UTimelineComponent>(this, UTimelineComponent::StaticClass());
AimTimeline->SetLooping(false);
AimTimeline->SetTimelineLength(0.5f);
AimTimeline->SetTimelineCurve(AimBlendCurve);
FOnTimelineFloat OnAimProgressDelegate;
OnAimProgressDelegate.BindUFunction(this, "OnAimProgress");
AimTimeline->AddInterpFloat(AimBlendCurve, OnAimProgressDelegate);
FOnTimelineEvent OnAimFinishedDelegate;
OnAimFinishedDelegate.BindUFunction(this, "OnAimFinished");
AimTimeline->SetTimelineFinishedFunc(OnAimFinishedDelegate);
}
void AMyCharacter::StartAiming()
{
AimTimeline->PlayFromStart();
}
void AMyCharacter::StopAiming()
{
AimTimeline->Reverse();
}
void AMyCharacter::OnAimProgress(float Progress)
{
CurrentFOV = FMath::Lerp(DefaultFOV, AimingFOV, Progress);
FirstPersonCamera->SetFieldOfView(CurrentFOV);
}
void AMyCharacter::OnAimFinished()
{
if (AimTimeline->IsReversing())
{
CurrentFOV = DefaultFOV;
}
else
{
CurrentFOV = AimingFOV;
}
FirstPersonCamera->SetFieldOfView(CurrentFOV);
}
平台跳跃游戏
平台跳跃游戏(如《超级马里奥》、《索尼克》等)通常需要一个能够跟随角色并自动调整视角的摄像机。这种摄像机需要能够在角色跳跃时保持稳定的视角,并且在角色进入特定区域时能够自动调整视角。
摄像机跟随角色跳跃
在平台跳跃游戏中,摄像机需要跟随角色的跳跃动作,并且在跳跃过程中保持稳定的视野。这可以通过在角色的蓝图中添加摄像机平滑跟随逻辑来实现。以下是一个蓝图示例,展示了如何实现摄像机跟随角色跳跃的平滑效果:
// 在角色蓝图中添加摄像机平滑跟随逻辑
// 1. 打开角色蓝图编辑器
// 2. 在组件列表中添加一个Camera组件
// 3. 将Camera组件的位置设置在角色后方,例如X=0, Y=-300, Z=150
// 4. 在Event Graph中添加一个SmoothFollow函数,使用FInterpTo函数实现平滑跟随
// 代码示例:在角色的C++类中实现摄像机平滑跟随
// MyCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"
UCLASS()
class MYGAME_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
AMyCharacter();
UFUNCTION(BlueprintCallable, Category = "Camera")
void SmoothFollowCamera(float DeltaTime);
protected:
UCameraComponent* FollowCamera;
FVector TargetLocation;
float InterpSpeed;
};
// MyCharacter.cpp
#include "MyCharacter.h"
#include "Camera/CameraComponent.h"
AMyCharacter::AMyCharacter()
{
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(RootComponent);
FollowCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 150.0f));
FollowCamera->SetRelativeRotation(FRotator(-30.0f, 0.0f, 0.0f));
InterpSpeed = 10.0f;
}
void AMyCharacter::SmoothFollowCamera(float DeltaTime)
{
// 计算目标位置
TargetLocation = GetActorLocation() + FVector(-300.0f, 0.0f, 150.0f);
// 平滑跟随
FollowCamera->SetWorldLocation(FMath::VInterpTo(FollowCamera->GetWorldLocation(), TargetLocation, DeltaTime, InterpSpeed));
}
摄像机自动调整视角
在平台跳跃游戏中,当角色进入特定区域时,摄像机需要自动调整视角,以便玩家能够更好地观察游戏环境。这可以通过在关卡蓝图中添加触发器和摄像机调整逻辑来实现。以下是一个蓝图示例,展示了如何实现摄像机自动调整视角:
// 在关卡蓝图中添加触发器和摄像机调整逻辑
// 1. 打开关卡蓝图编辑器
// 2. 在关卡中添加一个Trigger Box
// 3. 在Trigger Box的Event BeginOverlap节点中,调用角色的调整摄像机函数
// 代码示例:在角色的C++类中实现摄像机自动调整视角
// MyCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"
UCLASS()
class MYGAME_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
AMyCharacter();
UFUNCTION(BlueprintCallable, Category = "Camera")
void AdjustCameraForArea(FVector NewTargetLocation, FRotator NewTargetRotation);
protected:
UCameraComponent* FollowCamera;
};
// MyCharacter.cpp
#include "MyCharacter.h"
#include "Camera/CameraComponent.h"
AMyCharacter::AMyCharacter()
{
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(RootComponent);
FollowCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 150.0f));
FollowCamera->SetRelativeRotation(FRotator(-30.0f, 0.0f, 0.0f));
}
void AMyCharacter::AdjustCameraForArea(FVector NewTargetLocation, FRotator NewTargetRotation)
{
// 调整摄像机位置和旋转
FollowCamera->SetWorldLocation(NewTargetLocation);
FollowCamera->SetWorldRotation(NewTargetRotation);
}
开放世界游戏
开放世界游戏(如《荒野大镖客2》、《塞尔达传说:旷野之息》等)通常需要一个能够适应广阔游戏世界的摄像机。这种摄像机需要能够在不同的环境中自动调整视角,并且提供多种视角切换功能,以便玩家能够更好地探索和观察游戏世界。
摄像机动态调整
在开放世界游戏中,摄像机需要根据角色的位置和环境动态调整视角,以避免视角被地形或物体遮挡。这可以通过在角色的蓝图中添加摄像机动态调整逻辑来实现。以下是一个蓝图示例,展示了如何实现摄像机动态调整:
// 在角色蓝图中添加摄像机动态调整逻辑
// 1. 打开角色蓝图编辑器
// 2. 在Event Graph中添加一个Tick节点
// 3. 在Tick节点中调用一个自定义函数,用于调整摄像机位置和旋转
代码示例:在角色的C++类中实现摄像机动态调整
// MyCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"
UCLASS()
class MYGAME_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
AMyCharacter();
UFUNCTION(BlueprintCallable, Category = "Camera")
void AdjustCameraDynamic(float DeltaTime);
protected:
UCameraComponent* FollowCamera;
FVector DefaultTargetLocation;
FRotator DefaultTargetRotation;
float AdjustSpeed;
};
// MyCharacter.cpp
#include "MyCharacter.h"
#include "Camera/CameraComponent.h"
AMyCharacter::AMyCharacter()
{
// 设置默认摄像机位置和旋转
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(RootComponent);
FollowCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 150.0f));
FollowCamera->SetRelativeRotation(FRotator(-30.0f, 0.0f, 0.0f));
// 设置默认目标位置和旋转
DefaultTargetLocation = FVector(-300.0f, 0.0f, 150.0f);
DefaultTargetRotation = FRotator(-30.0f, 0.0f, 0.0f);
// 设置调整速度
AdjustSpeed = 10.0f;
}
void AMyCharacter::AdjustCameraDynamic(float DeltaTime)
{
// 获取当前角色位置
FVector CurrentLocation = GetActorLocation();
// 计算新的目标位置
FVector NewTargetLocation = CurrentLocation + DefaultTargetLocation;
// 获取当前摄像机位置
FVector CurrentCameraLocation = FollowCamera->GetWorldLocation();
// 使用插值函数平滑调整摄像机位置
FVector SmoothedLocation = FMath::VInterpTo(CurrentCameraLocation, NewTargetLocation, DeltaTime, AdjustSpeed);
FollowCamera->SetWorldLocation(SmoothedLocation);
// 获取当前角色旋转
FRotator CurrentRotation = GetActorRotation();
// 计算新的目标旋转
FRotator NewTargetRotation = CurrentRotation + DefaultTargetRotation;
// 获取当前摄像机旋转
FRotator CurrentCameraRotation = FollowCamera->GetWorldRotation();
// 使用插值函数平滑调整摄像机旋转
FRotator SmoothedRotation = FMath::RInterpTo(CurrentCameraRotation, NewTargetRotation, DeltaTime, AdjustSpeed);
FollowCamera->SetWorldRotation(SmoothedRotation);
}
摄像机视角切换
开放世界游戏通常需要提供多种视角切换功能,以便玩家能够更好地观察和探索游戏世界。例如,玩家可以在自由探索时切换到第三人称视角,在战斗时切换到第一人称视角。以下是一个蓝图示例,展示了如何实现摄像机视角切换:
// 在角色蓝图中添加输入动作绑定
// 1. 打开角色蓝图编辑器
// 2. 进入Event Graph
// 3. 添加一个Input Action节点,例如命名为"TogglePerspective"
// 4. 将"TogglePerspective"节点连接到一个自定义函数
代码示例:在角色的C++类中实现摄像机视角切换
// MyCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"
UCLASS()
class MYGAME_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
AMyCharacter();
UFUNCTION(BlueprintCallable, Category = "Camera")
void TogglePerspective();
protected:
UCameraComponent* FirstPersonCamera;
UCameraComponent* ThirdPersonCamera;
bool bIsFirstPerson;
};
// MyCharacter.cpp
#include "MyCharacter.h"
#include "Camera/CameraComponent.h"
AMyCharacter::AMyCharacter()
{
// 设置默认摄像机
FirstPersonCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
FirstPersonCamera->SetupAttachment(GetMesh(), TEXT("head"));
FirstPersonCamera->SetRelativeLocation(FVector(0.0f, 0.0f, 100.0f));
FirstPersonCamera->SetRelativeRotation(FRotator(0.0f, 0.0f, 0.0f));
ThirdPersonCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ThirdPersonCamera"));
ThirdPersonCamera->SetupAttachment(RootComponent);
ThirdPersonCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 150.0f));
ThirdPersonCamera->SetRelativeRotation(FRotator(-30.0f, 0.0f, 0.0f));
// 默认为第三人称视角
bIsFirstPerson = false;
ThirdPersonCamera->Activate();
FirstPersonCamera->Deactivate();
}
void AMyCharacter::TogglePerspective()
{
if (bIsFirstPerson)
{
// 切换到第三人称视角
ThirdPersonCamera->Activate();
FirstPersonCamera->Deactivate();
bIsFirstPerson = false;
}
else
{
// 切换到第一人称视角
FirstPersonCamera->Activate();
ThirdPersonCamera->Deactivate();
bIsFirstPerson = true;
}
}
摄像机碰撞检测
在开放世界游戏中,摄像机需要进行碰撞检测,以避免被地形或物体遮挡。这可以通过在角色的蓝图中添加摄像机碰撞组件并设置相应的逻辑来实现。以下是一个蓝图示例,展示了如何实现摄像机碰撞检测:
// 在角色蓝图中添加摄像机碰撞组件
// 1. 打开角色蓝图编辑器
// 2. 在组件列表中添加一个Camera Collision Component
// 3. 设置碰撞检测逻辑,当摄像机被遮挡时调整其位置
代码示例:在角色的C++类中实现摄像机碰撞检测
// MyCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Components/SphereComponent.h"
#include "MyCharacter.generated.h"
UCLASS()
class MYGAME_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
AMyCharacter();
UFUNCTION(BlueprintCallable, Category = "Camera")
void AdjustCameraForCollision();
protected:
UCameraComponent* FollowCamera;
USphereComponent* CameraCollisionSphere;
FVector DefaultTargetLocation;
FRotator DefaultTargetRotation;
float AdjustSpeed;
};
// MyCharacter.cpp
#include "MyCharacter.h"
#include "Camera/CameraComponent.h"
#include "Components/SphereComponent.h"
AMyCharacter::AMyCharacter()
{
// 设置默认摄像机
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(RootComponent);
FollowCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 150.0f));
FollowCamera->SetRelativeRotation(FRotator(-30.0f, 0.0f, 0.0f));
// 设置默认目标位置和旋转
DefaultTargetLocation = FVector(-300.0f, 0.0f, 150.0f);
DefaultTargetRotation = FRotator(-30.0f, 0.0f, 0.0f);
// 设置调整速度
AdjustSpeed = 10.0f;
// 添加摄像机碰撞组件
CameraCollisionSphere = CreateDefaultSubobject<USphereComponent>(TEXT("CameraCollisionSphere"));
CameraCollisionSphere->SetupAttachment(FollowCamera);
CameraCollisionSphere->SetSphereRadius(50.0f);
// 设置碰撞响应
CameraCollisionSphere->SetCollisionProfileName(TEXT("Camera"));
CameraCollisionSphere->OnComponentBeginOverlap.AddDynamic(this, &AMyCharacter::AdjustCameraForCollision);
}
void AMyCharacter::AdjustCameraForCollision()
{
// 获取当前角色位置
FVector CurrentLocation = GetActorLocation();
// 计算新的目标位置,使摄像机远离碰撞物体
FVector NewTargetLocation = CurrentLocation + FVector(-500.0f, 0.0f, 200.0f);
// 使用插值函数平滑调整摄像机位置
FVector SmoothedLocation = FMath::VInterpTo(FollowCamera->GetWorldLocation(), NewTargetLocation, GetWorld()->GetDeltaSeconds(), AdjustSpeed);
FollowCamera->SetWorldLocation(SmoothedLocation);
}
摄像机自由旋转
开放世界游戏的玩家通常需要能够自由旋转摄像机,以便更灵活地观察环境。这可以通过在角色的蓝图中添加输入绑定和摄像机旋转逻辑来实现。以下是一个蓝图示例,展示了如何实现摄像机自由旋转:
// 在角色蓝图中添加输入绑定
// 1. 打开角色蓝图编辑器
// 2. 进入Event Graph
// 3. 添加一个Input Axis节点,例如命名为"MouseX"和"MouseY"
// 4. 将"MouseX"和"MouseY"节点的输出连接到摄像机的旋转调整
代码示例:在角色的C++类中实现摄像机自由旋转
// MyCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"
UCLASS()
class MYGAME_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
AMyCharacter();
protected:
UCameraComponent* FollowCamera;
float MouseSensitivityX;
float MouseSensitivityY;
// 输入旋转视角
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
// 旋转摄像机
void RotateCamera(float DeltaTime, float XAxisValue, float YAxisValue);
};
// MyCharacter.cpp
#include "MyCharacter.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/PlayerController.h"
#include "GameFramework/SpringArmComponent.h"
AMyCharacter::AMyCharacter()
{
// 设置默认摄像机
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(RootComponent);
FollowCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 150.0f));
FollowCamera->SetRelativeRotation(FRotator(-30.0f, 0.0f, 0.0f));
// 设置鼠标灵敏度
MouseSensitivityX = 1.0f;
MouseSensitivityY = 1.0f;
}
void AMyCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// 绑定输入轴
PlayerInputComponent->BindAxis("MouseX", this, &AMyCharacter::AddControllerYawInput);
PlayerInputComponent->BindAxis("MouseY", this, &AMyCharacter::AddControllerPitchInput);
}
void AMyCharacter::RotateCamera(float DeltaTime, float XAxisValue, float YAxisValue)
{
// 旋转视角
AddControllerYawInput(XAxisValue * MouseSensitivityX * DeltaTime);
AddControllerPitchInput(YAxisValue * MouseSensitivityY * DeltaTime);
}
通过以上示例,我们可以看到不同游戏类型对虚拟摄像机的需求和实现方式各有不同。在第三人称动作游戏中,摄像机需要跟随角色并提供多种视角切换功能;在第一人称射击游戏中,摄像机需要位于角色头部并实现平滑的瞄准过渡;在平台跳跃游戏中,摄像机需要跟随角色跳跃并自动调整视角;在开放世界游戏中,摄像机需要动态调整视角,进行碰撞检测,并提供自由旋转功能。这些实现方法可以帮助开发者更好地设计和实现虚拟摄像机,提升游戏的玩家体验。