一、C++创建

1.      创建一个C++类,继承自Character,取名MyThirdCharacter

2.      在头文件中声明一个摄像机目标点CameraBoom,一个摄像机FollowCamera,再声明两个控制移动的函数,与跳跃的函数

#include "GameFramework/Character.h"
#include "MyThirdCharacter.generated.h" UCLASS()
class MYFIRSTNULLPROJECT_API AMyThirdCharacter : public ACharacter
{
GENERATED_BODY() //摄像机目标点
class USpringArmComponent* CameraBoom;
//跟随相机
class UCameraComponent* FollowCamera;
public:
// Sets default values for this character's properties
AMyThirdCharacter(); // Called when the game starts or when spawned
virtual void BeginPlay() override; // Called every frame
virtual void Tick( float DeltaSeconds ) override; // Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override; //控制前后移动
void MoveForward(float value);
//控制左右移动
void MoveRight(float value);
};

3.      在构造函数中,初始化一些控制参数与创建自己所需要的组件

4.      在输入中,处理控制函数

#include "MyFirstNULLProject.h"
#include "MyThirdCharacter.h" // Sets default values
AMyThirdCharacter::AMyThirdCharacter()
{
// 设置这个Character每帧调用Tick函数
PrimaryActorTick.bCanEverTick = true;
//设置控制器不受旋转影响
bUseControllerRotationPitch = false;
bUseControllerRotationRoll = false;
bUseControllerRotationYaw = false;
//设置胶囊碰撞体的大小
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.f);
//设置控制器在移动时可以旋转
GetCharacterMovement()->bOrientRotationToMovement = true;
//在本地找到模型文件,并赋值给mesh设置相应参数
static ConstructorHelpers::FObjectFinder<USkeletalMesh> playerPawn(TEXT("/Game/Animation/mon_goblinWizard/mon_goblinWizard"));
GetMesh()->SetSkeletalMesh(playerPawn.Object);
GetMesh()->SetRelativeLocation(FVector(0, 0, -95.f));
GetMesh()->SetRelativeRotation(FRotator(0,-90.f,0.f));
//创建一个摄像机目标点 USpringArmComponent 设置父级 长度 可旋转
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->AttachTo(RootComponent);
CameraBoom->TargetArmLength = 300;
CameraBoom->bUsePawnControlRotation = true;
//创建一个摄像机 绑定到CameraBoom下,不受控制旋转的影响 受CameraBoom旋转影响
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->AttachTo(CameraBoom, USpringArmComponent::SocketName);
FollowCamera->bUsePawnControlRotation = false; } // Called when the game starts or when spawned
void AMyThirdCharacter::BeginPlay()
{
Super::BeginPlay();
//设置初始位置
RootComponent->SetWorldLocation(FVector(0, 0, 0));
} // Called every frame
void AMyThirdCharacter::Tick( float DeltaTime )
{
Super::Tick( DeltaTime ); } // Called to bind functionality to input
//处理输入事件
void AMyThirdCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
InputComponent->BindAxis("MoveForward", this, &AMyThirdCharacter::MoveForward);
InputComponent->BindAxis("MoveRight", this, &AMyThirdCharacter::MoveRight);
InputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
InputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput); InputComponent->BindAction("JumpBtn",IE_Pressed,this, &ACharacter::Jump);
InputComponent->BindAction("JumpBtn", IE_Released,this, &ACharacter::StopJumping);
} void AMyThirdCharacter::MoveForward(float value)
{
if ((Controller != NULL) && (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);
}
} void AMyThirdCharacter::MoveRight(float value)
{
if ((Controller != NULL) && (value != 0.0f))
{
// 找到当前的前方向
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0); // 获取到右方向
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(Direction, value);
}
}

5.      设置GameMode的DefaultPawnClass,编译运行

二、蓝图创建

1.创建一个新的蓝图,继承自Character,取名MyThirdHero

2.双击打开蓝图,选择MyThirdHero,确定bUseControllerRotationPitch,bUseControllerRotationRoll,bUseControllerRotationYaw均为false,不受旋转影响

3.选中mesh,在SkeletalMesh中,选择自己的导入的模型,并设置位置旋转等相应参数,可在Viewport中看见详细情况

4.选中CapsuleComponent,在Shape栏下,可以根据模型大小调整碰撞胶囊的大小、

5.创建一个摄像机目标点,点击AddComponent,在Camera分类中有一个Spring Arm,点击创建,选中在右边可以设置他的参数,比如TargetArm
Length则是,他与摄像机的距离,这里主要还得勾选UsePawn Control Rotation,让他可以旋转

6.创建一个摄像机,让他作为SpringArm的子节点,确定摄像机的Use Pawn Control Rotation为不勾选状态,摄像机跟随父节点的旋转

7.选中CharacterMovement,确定OrientRotationtoMovement为勾选状态,让他可以在移动的时候旋转

8.开始编写输入控制,让角色可以动起来

9.编译,设置GameMode的DefaultPawnClass,编译,运行

UE4 创建第三人称角色的更多相关文章

  1. UE4中创建第一、第三人称角色,并进行角色间的切换

    在游戏中经常会出现第一人称和第三人称的视角切换场景,笔者在这里简单介绍如何进行这步操作. 1.创建角色 在内容浏览器中添加2个Character蓝图,分别命名为FirstPersonalCharact ...

  2. 【Unity】第11章 第三人称角色控制器和碰撞体

    分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 第三人称视角控制器涉及的相关概念有: 1.刚体(Rigidbody). 2.碰撞体(Collider).包括球体碰撞体( ...

  3. UE4 创建自己的角色

    首先,需要在UE4中设置自己需要输入的按键,点击工具栏的Settings/ProjectSettings,找到input,在Bindings下添加自己需要的输入按键,我这里绑定了JumpBtn.Mov ...

  4. 第三人称角色移动及自由移动视野(RigidBody实现)

    重点:向量的运算.在获得水平及垂直方向的速度之后,将方向进行重设,让方向与视野同步(即:相机的方向与人物方向相同) 下面以一个实例来说明如何操作: 1.如图创建一个地形(Terrain),两个正方体( ...

  5. 3D游戏开发之在UE4中创建非玩家角色(NPC)

    接着上节我们继续学习,现在我们来创建一些NPC(non-playable characters,非玩家角色).在这个游戏中,当我们靠近NPC时,它们会做出相应的反应. 一 创建C++类 1) 在UE编 ...

  6. [UE4]使用Is Locally Controlled解决第一人称和第三人称武器位置问题

    一.在第一人称网络游戏中,自己看到的是第一人称,其他玩家看到的自己是第三人称. 二.由于第一人称和第三人称是不同的模型,所以枪在模型上面的插槽位置也会不一样. 三.在武器挂载在人物模型的使用,使用“I ...

  7. Unity3D中的第三人称镜头的脚本控制

    原地址:http://blog.csdn.net/mobanchengshuang/article/details/27591271 好久没有敲Blog了,谢谢大家的留言.关注.私信等支持,但是我好像 ...

  8. Unity3D第三人称摄像机控制脚本

    好久没有敲Blog该.感谢您的留言.注意.私人信件和其他支持,但我似乎没有办法继续自己曾经写了一篇博客系列,因为我在网上找到有关unity3D太少的内容,U3D相关的文章!.. 第三人称视角 第三人称 ...

  9. Unity 3D还原Scene场景、市面多数游戏视角高度自定义、第三人称视角分离功能:平移、拖动、看向中心等

    Unity视角的高度自定义 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享. ...

随机推荐

  1. 20160213.CCPP体系详解(0023天)

    程序片段(01):全排列.c 内容概要:全排列密码库 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <std ...

  2. 打开Voice Over时,CATextLayer的string对象兼容NSString和NSAttributedString导致的Crash(二解决思路3)

    续前一篇:打开Voice Over时,CATextLayer的string对象兼容NSString和NSAttributedString导致的Crash(二解决思路2)ok,到这里已经能够锁定范围了, ...

  3. linuxsvn源代码版本库建立

    linuxsvn源代码版本库建立 下面就要建立代码的版本库做描述: 1.     安装svn版本服务器端 yum install subversion 从镜像下载安装svn服务器端,我们服务器已经安装 ...

  4. JAVA面向对象-----接口的特点

    接口的特点 1.类实现接口可以通过implements实现,实现接口的时候必须把接口中的所有方法实现,一个类可以实现多个接口. 2.接口中定义的所有的属性默认是public static final的 ...

  5. FORM的静态提交

     在form中进行保存时,如果使用commit_form的话会弹出信息提示"没有修改需要保存"或者"几条记录已保存"类似的字样,有时候不想被提示,可以使用A ...

  6. 【一天一道LeetCode】#374. Guess Number Higher or Lower

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 We are ...

  7. android问题:Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/24196143 Installation error: INSTALL_FAILED_C ...

  8. Linux上程序调试的基石(2)--GDB

    3. GDB的实现 GDB是GNU发布的一个强大的程序调试工具,用以调试C/C++程序.可以使程序员在程序运行的时候观察程序在内存/寄存器中的使用情况.它的实现也是基于ptrace系统调用来完成的.  ...

  9. 关于"net::ERR_CONNECTION_ABORTED"和"Firebug 达到了 Post 请求大小限制"的问题

    1.其中"net::ERR_CONNECTION_ABORTED"是在Chrome的控制台中打印出来的. 2."Firebug 达到了 Post 请求大小限制" ...

  10. LiveBlox无需代码的开发工具--支持win macos ubuntu等开发环境--

    LiveBlox无需代码的开发工具-支持windows macos ubuntu. 强大 灵活 易于使用 视频简介:LiveBlox Develop Technology Without Coding ...