UE4学习笔记2nd:玩家输入和Pawns

在这篇文章中,我会继续跟随官方文档进行虚幻4的C++编程练习,处理玩家的输入

首先,新建C++项目,附带初学者内容,命名为HowTo_PlayerInput
这里写图片描述

在内容浏览器中右键选择新建C++类,以Pawn为父类,命名为MyPawn
这里写图片描述
在打开的MyPawn.cpp中,在AMyPawn::AMyPawn()函数中添加一段代码,使之能够在游戏开始时能够响应玩家的输入
AutoPossesPlayer = EAutoReceiveInput::Player0;//设置为玩家控制
这里写图片描述

接着,添加一些基础组建,在MyPawn.h中添加
UPROPERTY(EditAnywhere)
USceneComponent* OurVisibleComponent;
添加在类定义的最后
这里写图片描述

这里写图片描述

这段话是说:UPROPERTY使它在引擎中可以被看到,它可以防止游戏启动时或项目的关闭和重新载入时的变量重置。
回到MyPawn.cpp,添加以下代码到AMyPawn::AMyPawn()中

//添加一个可以添加对象的空根组件
RootComponent = CreateDefaultSubobject(TEXT(“RootComponent”));
//创建相机和可见项目
UCameraComponent* OurCamera = CreateDefaultSubobject(TEXT(“OurCamera”));
OurVisibleComponent = CreateDefaultSubobject(TEXT(“OurVisibleComponent”));
//附加相机和可见对象到根组建,偏移并旋转相机。
OurCamera->AttachTo(RootComponent);
OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
OurVisibleComponent->AttachTo(RootComponent);
这里写图片描述

关闭MyPawn.cpp与MyPawn.h,右键选择HowTO_PlayerInput中进行编译
这里写图片描述

如果代码没有错误,输出栏会显示以下信息
这里写图片描述

即为编译成功

这一章的内容先进行到这里,剩余部分明天继续。

使用函数总结:

1,绑定输入需要继承Pawn,同时实现方法SetupPlayerInputComponent;

2,代码创建组件,CreateDefaultSubobject;

3,组件依赖,AttachTo;

4,设置位置,SetActorRotation
官方文档链接:https://docs.unrealengine.com/latest/INT/Programming/Tutorials/PlayerInput/1/index.html

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
In just 24 lessons of one hour or less, learn how to start using Unreal Engine 4 to build amazing games for Windows, Mac, PS4, Xbox One, iOS, Android, the web, Linux–or all of them! Sams Teach Yourself Unreal Engine 4 Game Development in 24 Hours’ straightforward, step-by-step approach shows you how to work with Unreal Engine 4’s interface, its workflows, and its most powerful editors and tools. In just hours you’ll be creating effects, scripting warfare, implementing physics–even developing for mobile devices and HUDs. Every lesson builds on what you’ve already learned, giving you a rock-solid foundation for real-world success. Organize new projects and work with the Gameplay Framework Master Unreal’s units and control systems Import 3D models and work with the Static Mesh Editor Create new landscapes and use Unreal’s foliage system Bring characters and creatures to life with the Persona Editor Apply materials and build lighting Integrate and modify audio with the Unreal Sound Cue Editor Craft particle effects and simulate physics Set up and react to player inputs Build levels and entirely new worlds Get started with powerful Blueprint visual scripting system Script an arcade game from start to finish Create events that respond to player actions Spawn Actors during gameplay Design and create action-based encounters Optimize games for mobile devices and touch-based inputs Build menus with Unreal’s UMG UI Designer Prepare your game for deployment Step-by-step instructions carefully walk you through the most common Unreal Engine 4 game development tasks. Practical, hands-on examples show you how to apply what you learn. Quizzes and Exercises help you test your knowledge and stretch your skills. Notes and tips point out shortcuts and solutions. All the project files and assets you'll need are available for download, including "before-and-after" files demonstrating initial setup and proper completion for every exercise. Table of Contents HOUR 1: Introducing Unreal E

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值