请选择 进入手机版 | 继续访问电脑版

01BIM社区

 找回密码
 立即注册

扫一扫,访问微社区

查看: 648|回复: 1

官方案例程序:Player_Controlled Camera

[复制链接]

111

主题

232

帖子

930

积分

高级会员

Rank: 4

积分
930
发表于 2018-3-28 07:33:59 | 显示全部楼层 |阅读模式
Player_Controlled Camera源代码:
一、HowTo_PlayerCamera.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Runtime/Engine/Classes/GameFramework/SpringArmComponent.h"
#include "Runtime/Engine/Classes/Camera/CameraComponent.h"
#include "awnWithCamera.generated.h"
UCLASS()
class HOWTO_PLAYERCAMERA_API APawnWithCamera : public APawn
{
        GENERATED_BODY()

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

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;

protected:
        UPROPERTY(EditAnywhere)
                USpringArmComponent* OurCameraSpringArm;
        UCameraComponent* OurCamera;
       
        //Input variables
        FVector2D MovementInput;
        FVector2D CameraInput;
        float ZoomFactor;
        bool bZoomingIn;

        //Input functions
        void MoveForward(float AxisValue);
        void MoveRight(float AxisValue);
        void PitchCamera(float AxisValue);
        void YawCamera(float AxisValue);
        void ZoomIn();
        void ZoomOut();
};
二、PawnWithCamera.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "awnWithCamera.h"
// Sets default values
APawnWithCamera::APawnWithCamera()
{
        // 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;
        //Create our components
        RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
        OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
        OurCameraSpringArm->SetupAttachment(RootComponent);
        OurCameraSpringArm->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 50.0f), FRotator(-60.0f, 0.0f, 0.0f));
        OurCameraSpringArm->TargetArmLength = 400.f;
        OurCameraSpringArm->bEnableCameraLag = true;
        OurCameraSpringArm->CameraLagSpeed = 3.0f;
        OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));
        OurCamera->SetupAttachment(OurCameraSpringArm, USpringArmComponent::SocketName);
        //Take control of the default Player
        AutoPossessPlayer = EAutoReceiveInput:layer0;
}
// Called when the game starts or when spawned
void APawnWithCamera::BeginPlay()
{
        Super::BeginPlay();       
}
// Called every frame
void APawnWithCamera::Tick(float DeltaTime)
{
        Super::Tick(DeltaTime);
        //Zoom in if ZoomIn button is down, zoom back out if it's not
        {
                if (bZoomingIn)
                {
                        ZoomFactor += DeltaTime / 0.5f;         //Zoom in over half a second
                }
                else
                {
                        ZoomFactor -= DeltaTime / 0.25f;        //Zoom out over a quarter of a second
                }
                ZoomFactor = FMath::Clamp<float>(ZoomFactor, 0.0f, 1.0f);
                //Blend our camera's FOV and our SpringArm's length based on ZoomFactor
                OurCamera->FieldOfView = FMath:erp<float>(90.0f, 60.0f, ZoomFactor);
                OurCameraSpringArm->TargetArmLength = FMath:erp<float>(400.0f, 300.0f, ZoomFactor);
        }

        //Rotate our actor's yaw, which will turn our camera because we're attached to it
        {
                FRotator NewRotation = GetActorRotation();
                NewRotation.Yaw += CameraInput.X;
                SetActorRotation(NewRotation);
        }

        //Rotate our camera's pitch, but limit it so we're always looking downward
        {
                FRotator NewRotation = OurCameraSpringArm->GetComponentRotation();
                NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);
                OurCameraSpringArm->SetWorldRotation(NewRotation);
        }

        //Handle movement based on our "MoveX" and "MoveY" axes
        {
                if (!MovementInput.IsZero())
                {
                        //Scale our movement input axis values by 100 units per second
                        MovementInput = MovementInput.SafeNormal() * 100.0f;
                        FVector NewLocation = GetActorLocation();
                        NewLocation += GetActorForwardVector() * MovementInput.X * DeltaTime;
                        NewLocation += GetActorRightVector() * MovementInput.Y * DeltaTime;
                        SetActorLocation(NewLocation);
                }
        }
}
// Called to bind functionality to input
void APawnWithCamera::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
        Super::SetupPlayerInputComponent(PlayerInputComponent);
        //Hook up events for "ZoomIn"
        InputComponent->BindAction("ZoomIn", IE_Pressed, this, &APawnWithCamera::ZoomIn);
        InputComponent->BindAction("ZoomIn", IE_Released, this, &APawnWithCamera::ZoomOut);
        //Hook up every-frame handling for our four axes
        InputComponent->BindAxis("MoveForward", this, &APawnWithCamera::MoveForward);
        InputComponent->BindAxis("MoveRight", this, &APawnWithCamera::MoveRight);
        InputComponent->BindAxis("CameraPitch", this, &APawnWithCamera:itchCamera);
        InputComponent->BindAxis("CameraYaw", this, &APawnWithCamera::YawCamera);
}
//Input functions
void APawnWithCamera::MoveForward(float AxisValue)
{
        MovementInput.X = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
}
void APawnWithCamera::MoveRight(float AxisValue)
{
        MovementInput.Y = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
}
void APawnWithCamera:itchCamera(float AxisValue)
{
        CameraInput.Y = AxisValue;
}
void APawnWithCamera::YawCamera(float AxisValue)
{
        CameraInput.X = AxisValue;
}
void APawnWithCamera::ZoomIn()
{
        bZoomingIn = true;
}
void APawnWithCamera::ZoomOut()
{
        bZoomingIn = false;
}


回复

使用道具 举报

111

主题

232

帖子

930

积分

高级会员

Rank: 4

积分
930
 楼主| 发表于 2018-3-28 07:44:35 | 显示全部楼层
18.jpg
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|01BIM社区 - 最专业的BIM技术交流平台 ( 渝ICP备15000873号 )

GMT+8, 2018-12-17 19:58 , Processed in 0.063334 second(s), 25 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表