当前位置:网站首页>[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.
边栏推荐
猜你喜欢
随机推荐
[Deep Learning] Today's bug (August 2)
Why do I strongly recommend using smart async?
【带你了解SDN和网络虚拟化】
C语言02、语句、函数
C专家编程 第1章 C:穿越时空的迷雾 1.9 阅读ANSI C标准,寻找乐趣和裨益
世界顶级级架构师编写2580页DDD领域驱动设计笔记,属实有牌面
CopyOnWriteArrayList details
QT QT 】 【 to have developed a good program for packaging into a dynamic library
机器人开发--Universal Scene Description(USD)
FinClip | 2022 年 7 月产品大事记
leetcode-693.交替位二进制数
C专家编程 第1章 C:穿越时空的迷雾 1.8 ANSI C标准的结构
元宇宙系列--Value creation in the metaverse
C专家编程 第2章 这不是Bug,而是语言特性 2.3 误做之过
可复现、开放科研、跨学科合作:数据驱动下的科研趋势及应用方案
leetcode:202. 快乐数
Looking at the ecological potential of Hongmeng OS from the evolution of MatePad Pro
STM32 GPIO LED and buzzer implementation [Day 4]
When mobile applications go overseas, is your "network optimization" holding back?
#夏日挑战赛# HarmonyOS 实现一个绘画板









