当前位置:网站首页>[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.
边栏推荐
猜你喜欢
Not to be ignored!Features and advantages of outdoor LED display
protobuf 反射使用总结
设置海思芯片MMZ内存、OS内存详解
[Unity Getting Started Plan] Basic Concepts (6) - Sprite Renderer Sprite Renderer
Common distributed theories (CAP, BASE) and consensus protocols (Gosssip, Raft)
Difference and performance comparison between HAL and LL library of STM32
一文看懂推荐系统:召回03:基于用户的协同过滤(UserCF),要计算用户之间的相似度
Cookie和Session的关系
元宇宙系列--Value creation in the metaverse
#夏日挑战赛#【FFH】OpenHarmony设备开发基础(四)启动流程
随机推荐
[Unity Getting Started Plan] Basic Concepts (7) - Input Manager & Input Class
Common distributed theories (CAP, BASE) and consensus protocols (Gosssip, Raft)
请问下这个hologres维表是被缓存了么?怎么直接Finished了
How to start an NFT collection
Looking at the ecological potential of Hongmeng OS from the evolution of MatePad Pro
怎么在opengauss中进行测试自己添加的新函数的性能(循环n次的运行时间)?
想进阿里?先来搞懂一下分布式事务
组件通信-父传子组件通信
leetcode-268.丢失的数字
C专家编程 第3章 分析C语言的声明 3.1 只有编译器才会喜欢的语法
C专家编程 第3章 分析C语言的声明 3.4 通过图标分析C语言的声明
C专家编程 第1章 C:穿越时空的迷雾 1.11 轻松一下---由编译器定义的Pragmas效果
组件通信--下拉菜单案例
大佬们。使用flink-cdc-sqlserver 2.2.0 版本读取sqlserver2008R
攻防世界----bug
[Unity Getting Started Plan] Basic Concepts (6) - Sprite Renderer Sprite Renderer
Analysis of ffplay video playback principle
How to analyze the weekly activity rate?
EA 改口,称单人游戏是产品组合中“非常重要的一部分”
元宇宙系列--Value creation in the metaverse