UE4 创建第三人称角色
一、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 创建第三人称角色的更多相关文章
- UE4中创建第一、第三人称角色,并进行角色间的切换
在游戏中经常会出现第一人称和第三人称的视角切换场景,笔者在这里简单介绍如何进行这步操作. 1.创建角色 在内容浏览器中添加2个Character蓝图,分别命名为FirstPersonalCharact ...
- 【Unity】第11章 第三人称角色控制器和碰撞体
分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 第三人称视角控制器涉及的相关概念有: 1.刚体(Rigidbody). 2.碰撞体(Collider).包括球体碰撞体( ...
- UE4 创建自己的角色
首先,需要在UE4中设置自己需要输入的按键,点击工具栏的Settings/ProjectSettings,找到input,在Bindings下添加自己需要的输入按键,我这里绑定了JumpBtn.Mov ...
- 第三人称角色移动及自由移动视野(RigidBody实现)
重点:向量的运算.在获得水平及垂直方向的速度之后,将方向进行重设,让方向与视野同步(即:相机的方向与人物方向相同) 下面以一个实例来说明如何操作: 1.如图创建一个地形(Terrain),两个正方体( ...
- 3D游戏开发之在UE4中创建非玩家角色(NPC)
接着上节我们继续学习,现在我们来创建一些NPC(non-playable characters,非玩家角色).在这个游戏中,当我们靠近NPC时,它们会做出相应的反应. 一 创建C++类 1) 在UE编 ...
- [UE4]使用Is Locally Controlled解决第一人称和第三人称武器位置问题
一.在第一人称网络游戏中,自己看到的是第一人称,其他玩家看到的自己是第三人称. 二.由于第一人称和第三人称是不同的模型,所以枪在模型上面的插槽位置也会不一样. 三.在武器挂载在人物模型的使用,使用“I ...
- Unity3D中的第三人称镜头的脚本控制
原地址:http://blog.csdn.net/mobanchengshuang/article/details/27591271 好久没有敲Blog了,谢谢大家的留言.关注.私信等支持,但是我好像 ...
- Unity3D第三人称摄像机控制脚本
好久没有敲Blog该.感谢您的留言.注意.私人信件和其他支持,但我似乎没有办法继续自己曾经写了一篇博客系列,因为我在网上找到有关unity3D太少的内容,U3D相关的文章!.. 第三人称视角 第三人称 ...
- Unity 3D还原Scene场景、市面多数游戏视角高度自定义、第三人称视角分离功能:平移、拖动、看向中心等
Unity视角的高度自定义 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享. ...
随机推荐
- WiFi文件上传框架SGWiFiUpload
背景 在iOS端由于文件系统的封闭性,文件的上传变得十分麻烦,一个比较好的解决方案是通过局域网WiFi来传输文件并存储到沙盒中. 简介 SGWiFiUpload是一个基于CocoaHTTPServer ...
- JAVA面向对象-----局部内部类
局部内部类 局部内部类概述:包含在外部类的函数中的内部类称之为局部内部类. 访问:可以在包含局部内部类的方法中直接创建局部内部类的对象调用局部内部类的成员. 注意:局部内部类只能访问所在函数的fana ...
- The packages can be overrided by Java Endorsed Standards
Endorsed Standards APIs The Endorsed Standards for Java SE constitute all classes and interfaces ...
- PLSQL程序编写杂烦数据表信息编写批量排版
--PLSQL程序编写杂烦数据表信息编写批量排版 SELECT 'cra.' || lower(t.column_name) ||',' FROM dba_tab_columns t WHERE t. ...
- Android性能提升之强引用、软引用、弱引用、虚引用使用
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52637333 背景:收到公众投稿 ...
- [openwrt] uci 的shell和lua接口
uci是openwrt上配置操作的接口,不管是自动化的shell脚本,还是使用luci来二次开发配置界面,都会用到这部分知识. uci提供了lua, shell, c接口,这里主要用到了前两种 she ...
- Android游戏开发之SurfaceView的使用-android学习之旅(五)
SurfaceView和View的区别 View是在ui主线程中,直接响应用户的操作,以及任务的分发,但是任务比较复杂会出现阻塞. SurfaceView则不会出现这种问题,以为它直接从内存等取得图像 ...
- scala学习笔记2(类,继承,抽象类)
class Person{ // _ 是占位符; var name : String = _ val age : Int = 27 // private[this] 定义的内容无法外部使用,起到保护作 ...
- Android的oom详解
Android的oom原因 1.资源对象没关闭造成的内存泄露,try catch finally中将资源回收放到finally语句可以有效避免OOM.资源性对象比如: 1-1,Cursor 1-2,调 ...
- LaTex计数器
记数器 绝大多数都与可以改变他们的命令有相同的名称 part chapter section subsection paragraph subparagraph page equation figur ...