当前位置:网站首页>[Unity Starter Plan] Making RubyAdventure01 - Player Creation & Movement
[Unity Starter Plan] Making RubyAdventure01 - Player Creation & Movement
2022-08-03 16:39:00 【flashinggg】
目录
Project official tutorial address
Download and import the project
1.1 Create players and scenarios
1.2 Learn about sprite renderers Sprite Renderer
1.3 Static and dynamic of sprites
2.1 Try creating a mobile script
关于UnityThe unit of distance to move in
UnityEnter the input settings in the system
2.2 Keyboard input controls movement
以单位/Seconds represent movement speed
前期准备
Project official tutorial address
Ruby's Adventure:2D 初学者 - Unity Learn
项目特点
可以让我们:
- 学会使用2D资源
- 学习简单的2D RPG 游戏制作
- Learn to create and control characters(Script)
- 学习设置sprite
- Learn to make some simple special effects
Download and import the project
The resource storage address used
2D Beginner: Tutorial Resources | 教程 | Unity Asset Store
下载并在Unity中导入即可.
The formal study begins.
1 创建玩家
The main character and the first script - Unity Learn
1.1 Create players and scenarios
Enter the official first section of the tutorial,And download the player protagonistRuby的图片,下载的格式是PNG.由于我们的项目是2D的,需要将Texture Type改成Sprite(精灵),才能被Unity所使用.
Create a new scene named MainScene,and will modify the typeRuby图片拖入MainScene场景中.
Unity中2Duse of materials
Just made a simple process of importing pictures,In fact, it is a one-time use2D素材的过程,Unity中2The use of the material usually goes through the following process
PNG(2D图片格式) ----> Sprite ----> GameObject
1.2 Learn about sprite renderers Sprite Renderer
RubyThis game object is in addition to the initial oneTransform变换组件,还会有一个Sprite Renderer组件.
I wrote another blog about its properties:【Unity入门计划】基本概念(6)-精灵渲染器 Sprite Renderer
1.3 Static and dynamic of sprites
We now pass import onePNG转换成sprite后,得到的Ruby是一个静态精灵,The teacher also introduced in the course动态精灵,Learn more about this later.
2 移动玩家
UntiyMobile words should be based onTransformcomponents operate,At this time, you need to hang a mobile script!Created in the scriptRuby添加运动.
2.1 Try creating a mobile script
to the game object that was just createdRuby新建一个RubyController脚本.
Try hanging one firstxScript for automatic axis movement,看看情况.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RubyController : MonoBehaviour
{
// 每帧都会执行一次Update函数
void Update()
{
//transform获取当前游戏对象的transform组件的值
Vector2 position = transform.position;
position.x += 0.1f;
//Then assign it to the game objecttransform
transform.position = position;
}
}
关于UnityThe unit of distance to move in
When setting up the game object,经常会有“Set the object position to x轴XX个单位,y轴XX个单位”,UnityIt does not specify how long it is1米/1厘米,Just make your own rules as needed.
In-game controls
- 鼠标键盘
- 手机触屏
- gamepad
- 体感游戏(水果忍者
- 可穿戴设备:VR
- 声控
The game created this time will control the player's movement through keyboard input,此时就需要使用到Unity的输入系统.Input system includes输入设置和输入代码.
UnityEnter the input settings in the system
Just enter the settingsUnityDefines the role of the keys on the keyboard,可以由Edit -> Project Settings -> Input Manager查看默认设置.
关于Input Manager官方介绍文档:Input Manager - Unity 手册
对于Input Manager和InputClass I made a simple learning summary:【Unity入门计划】基本概念(7)-Input Manager&Input类
2.2 Keyboard input controls movement
It's no longer what it used to be,让RubyMoved by itself frame by frame,Now by callingAPIway to achieve keyboard input controlRuby的移动.
achieve horizontal control
需要修改脚本中的Update函数:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RubyController : MonoBehaviour
{
// 每帧都会执行一次Update函数
void Update()
{
//声明新变量
//Get the horizontal axis and store in a new variable
float horizontal = Input.GetAxis("Horizontal");
Debug.Log(horizontal);
Vector2 position = transform.position;
//对xThe axis travel distance has been slightly changed
position.x += horizontal * 0.1f;
transform.position = position;
}
}
这里实现了:
- 按下左键,轴为-1,则Ruby向左移动0.1f
- 按下右键,轴为1,则Ruby向右移动0.1f
- 不按任何键,轴为0,Ruby原地不动
Achieve vertical control
对于yAxes also add corresponding operations:
void Update()
{
//声明新变量
//Get the horizontal axis and store in a new variable
float horizontal = Input.GetAxis("Horizontal");
//Get the vertical axis
float vertical = Input.GetAxis("Vertical");
Vector2 position = transform.position;
//对xThe axis travel distance has been slightly changed
position.x += horizontal * 0.1f;
//对yThe axes are also changed accordingly
position.y += vertical * 0.1f;
transform.position = position;
}
Adjust time and frame rate
It can be improved by adjusting whether to enable vertical synchronization and limiting the frame rate/Decrease the game frame rateFPS.
For frame rate settings, please refer to the following articles:
Unity3d 帧率设置 及在游戏执行时显示帧率 - lcchuguo - 博客园 (cnblogs.com)
以单位/Seconds represent movement speed
目前,由于是在Update()updated in the functionRuby的位置信息,因此当前RubyThe movement speed is in units/帧,How to get him to flat/It runs at the speed of seconds?
需要Multiply the speed by UnityThe time it takes to render a frameto change the movement speed,例如:如果当前Unity以每秒10Frame rate rendering,It takes time per frame0.1秒,乘以0.1After seconds the movement speed becomes the unit/秒.
Unity中Time.deltaTime这个APICan help us get from the last frame to the current frame(只读)的间隔时间,其中deltaTime是Time中的一个变量,Indicates the delta time.
加上后:
void Update()
{
//声明新变量
//Get the horizontal axis and store in a new variable
float horizontal = Input.GetAxis("Horizontal");
//Get the vertical axis
float vertical = Input.GetAxis("Vertical");
Vector2 position = transform.position;
//对xThe axis travel distance has been slightly changed
position.x += horizontal * 0.1f*Time.deltaTime;
//对yThe axes are also changed accordingly
position.y += vertical * 0.1f*Time.deltaTime;
transform.position = position;
}
运行游戏,会发现RubyThe movement speed has slowed down!Because we changed velocity to units/秒,This means that currently we are only moving per second0.1个单位,This will of course be slow.如果将0.1f改成3.f,The movement speed will be much faster.
边栏推荐
- EA 改口,称单人游戏是产品组合中“非常重要的一部分”
- 附录A 程序员工作面试的秘密
- #夏日挑战赛# HarmonyOS 实现一个绘画板
- 滑环安装注意事项
- QT QT 】 【 to have developed a good program for packaging into a dynamic library
- MarkDown常用代码片段和工具
- 面了个腾讯35k出来的,他让我见识到什么叫精通MySQL调优
- Analysis of ffplay video playback principle
- CPU个数_核心数_线程数之间的关系
- To participate in sweepstakes, incoming new programmers magazine welfare!
猜你喜欢
正向代理与反向代理
如何选择合适的导电滑环型号
数据中台“集存通用治”功能场景说明
TiKV & TiFlash accelerate complex business queries丨TiFlash application practice
Windows 事件查看器记录到 MYSQL
#夏日挑战赛# HarmonyOS 实现一个绘画板
Introduction to spark learning - 1
Interpretation of the 2021 Cost of Data Breach Report
How to start an NFT collection
中小微企业如何简单便捷、低成本实现数字化?360视觉云有妙招
随机推荐
Web3 安全风险令人生畏?应该如何应对?
node连接mongoose数据库流程
C专家编程 第3章 分析C语言的声明 3.2 声明是如何形成的
C专家编程 第2章 这不是Bug,而是语言特性 2.2 多做之过
Some optional strategies and usage scenarios for PWA application Service Worker caching
Kubernetes 笔记 / 入门 / 生产环境 / 用部署工具安装 Kubernetes / 用 kubeadm 启动集群 / 安装 kubeadm
正向代理与反向代理
ArkUI如何适配横竖屏
protobuf 中数据编码规则
#夏日挑战赛# HarmonyOS 实现一个绘画板
MySQL窗口函数
建造者模式/生成器模式
Tolstoy: There are only two misfortunes in life
TCP 可靠吗?为什么?
Looking at the ecological potential of Hongmeng OS from the evolution of MatePad Pro
MySQL窗口函数 OVER()函数介绍
Common distributed theories (CAP, BASE) and consensus protocols (Gosssip, Raft)
【系统学习编程-编程入门-全民编程 视频教程】
MySQL查询语法
高薪程序员&面试题精讲系列132之微服务之间如何进行通信?服务熔断是怎么回事?你熟悉Hystrix吗?