ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

Unity 5 官方教程 #2 – 滚球游戏 – 玩家移动 – 译学馆
未登陆,请登陆后再发表信息
最新评论 (0)
播放视频

Unity 5 官方教程 #2 - 滚球游戏 - 玩家移动

Unity 5 - Roll a Ball game - 2 of 8: Moving the Player - Unity Official Tutorials

Now we are going to move the player game object.
现在我们移动玩家游戏对象
First let’s think through how we want this player game object to behave.
首先考虑我们希望玩家游戏对象有哪些功能
We want to have the sphere roll around on the game area,
我们想让球体在游戏区滚动
bump in to walls, stay on the ground
当小球碰撞到墙壁 就会停下
and not fly off in to space.
而不是飞到天上去
We want to be able to collide with our collectible game objects
我们想要小球和可收集对象发生碰撞
and pick them up when we do.
然后将他们捡起来
This requires physics.
这就需要物理引擎
To use physics the game object needs a rigidbody component attached.
为了受物理引擎作用 游戏对象需要附加一个刚体组件
To attach a new component we must first select the game object
为了附加新组件 我们首先得选中
we want to attach the component to.
我们想要附加组件的游戏对象
In this case we will select our player game object.
在本次示例中 我们会先选中玩家游戏对象
Then we can either choose the Component menu
然后我们可以选择 “组件” 菜单
and select Physics – Rigid Body
选中 “物理-刚体”
which would attach this component to the game object we have selected.
这就给被选中游戏对象添加了组件
Or use the Add Component button in the Inspector
也可以使用 “检查器” 面板中的 “添加组件” 按钮
choosing Physics – Rigid Body.
选择 “物理-刚体”
Either way this attached the rigidbody component to the selected game object.
也同样可以给被选中游戏对象添加刚体组件
If we choose to,
如果选好了
we can rearrange the order of the components on the game object
通过每个组件右上角的上下文相关齿轮菜单
using the context sensitive gear menu
我们可以对游戏对象上的组件
in the upper right of the component.
进行重新排序
Doing this has no effect on the performance of our game.
改变顺序并不会影响游戏的性能
However, having a consistent order to the components on our game object
但是游戏对象上的组件有一个连贯的顺序
may help us speed up our development
可以提高我们的开发效率
by keeping or maintaining an organised project and hierarchy.
保持维护一个有组织的项目和层次结构
Don’t forget, you can collapse or expand the Component view
不要忘了 你可以通过点击组件的标题栏
by clicking on the component’s title bar.
折叠或展开 “组件” 视图
We should note that whenever we do this,
要注意的是 每当我们切换的时候
this will also toggle the relevant gizmos for that component in the Scene view.
“场景”视图中 组件相应的线框也会变化
Now we need to get the player object moving under our control.
现在 我们要让玩家对象在我们的控制下移动
To do this we need to get input from our player through the keyboard
为了做到这点 我们要先获得玩家在键盘上的输入
and we need to apply that input to the player game object as forces
然后根据输入对玩家游戏对象施力
to move the sphere in the scene.
来让小球在场景中移动
We will do this by using a script
我们可以通过给玩家对象添加一个脚本
that we attach to the player game object.
来做到这一点
First let’s create a folder in our project view
首先在“项目视图”中创建一个文件夹
to hold our script assets.
用来存放脚本资源
In the Project view click on the Create menu
在 “项目视图” 中点击 “创建” 菜单
and choose Create Folder.
然后选择 “创建文件夹”
Rename this folder Scripts.
将文件夹重命名为 “脚本”
Next let’s create a new C# script.
接着再创建一个新的C#脚本
To create a new script we have some choices.
有几种方法可以创建新脚本
We can choose Assets-Create to create our new C# Script.
我们可以选择 “资源” —— “创建”
Or we can use the Create menu in the Project view.
也可以在 “项目视图” 中使用 “创建” 菜单
But what might be more efficient in this case
但这里最高效的方法是
would be to select the player game object
选中玩家游戏对象
and use the Add Component button in the Inspector.
使用 “检查器” 中的 “添加组件” 按钮
The Add Component menu contains the selection New Script.
“添加组件” 菜单包含了选项 “新脚本”
This allows us to both create and attach
我们就可以一步到位
a script in one step.
创建并添加新脚本了
First let’s name this script PlayerController.
首先我们将脚本命名为 “PlayerController”
We can choose the language of the script,
可以选择脚本语言
which will be C #,
选择C#语言
and then click on Create and Add.
然后点击 “创建” 和 “添加”
Or simply hit the return or enter key to confirm our selection.
或直接按回车键确定
Unity will create, compile and attach this script
Unity引擎会创建 编译这个脚本
to our selected game object.
并将其添加到选定游戏对象上
We should note, this way of creating a script
要注意 这种创建脚本的方式
will create that script asset on the root
会将脚本资源创建在 “项目视图” 的根目录里
or top level of our Project view.
或者 “项目视图” 的顶部
We will need to move the asset
我们需要把资源手动拖动到
in to the Scripts directory
“脚本” 文件夹里
in order to maintain an organised Project view.
以保持一个有组织的“项目视图”
If we select the script in our Project view
如果我们选中 “项目视图” 里的脚本
we see a preview of the script asset in the Inspector,
可以在 “检查器” 中查看脚本资源预览
but this text is not editable.
但这段文本是不可编辑的
Let’s open the script.
让我们打开这个脚本
We can do this a number of ways.
打开方式有很多种
When we are inspecting a game object using the script
当我们使用脚本检查游戏对象时
we can use the context sensitive gear menu.
可以使用上下文相关齿轮菜单
We can double click on the script asset in the Project view
当我们在 “项目视图” 中选中脚本时
or we can use the Open button in the Inspector
可以双击 “项目视图” 里的脚本资源
when we have the script selected in our Project view.
或者点击 “检查器” 里的 “打开” 按钮
This will open our script in our preferred script editor.
这样会在首选的脚本编辑器中打开脚本
First let’s remove the sample code provided in the base script.
首先 我们删掉基础脚本提供的样本代码
Next let’s think, what do we want to do with this script?
接着 想想我们想要用脚本做些什么?
We want to check every frame for player input
我们希望检查每一帧玩家输入
and then we want to apply that input
然后应用每帧输入
to the player game object every frame as movement.
作为玩家游戏对象的运动
Where will we check for and apply this input?
不过我们要在哪里检查和应用输入呢?
We have two choices.
两个选择:
Update and Fixed Update.
Update函数和Fixed Update函数
Update is called before rendering a frame
Update被调用在每次渲染一帧之前
and this is where most of our game code will go.
我们大部分的代码都会放在这里
Fixed Update on the other hand
另一方面 FixedUpdate函数
is called just before performing any physics calculations
仅仅是被调用于执行物理计算之前
and this is where our physics code will go.
我们的物理代码就放这里
We will be moving our ball by applying forces to the rigidbody,
我们会通过对刚体施力移动小球
this is physics.
这是物理学的
So we will put our code in Fixed Update.
所以我们选择FixedUpdate函数
What code do we need to write?
我们要编写什么代码呢?
We know we need Input, but how do we find out more?
我们知道要写一个 “Input” 但还有呢?
There is a shortcut in Monodevelop
在MonoDevelop编辑器里有一个快捷键
that searches the Unity API.
用于搜索Unity的API
On the Mac it’s command plus single quote,
在Mac系统上是cmd键加上单引号
and on the PC it’s control plus single quote.
在Windows系统上是ctrl键加单引号
Select the item you want to research,
选中想要搜索的目标
in our case Input,
这里是 “Input”
and hold down the command or control key
按住cmd键和ctrl键
and type the single quote button.
然后输入单引号
This search brings up every reference
会搜索出文档中
in our documentation related to input.
所有 “input” 的有关内容
Select Input at the top, interface into the input system.
选中顶部的 “Input” 进入输入系统
This brings up the page on Input.
出现 “Input”页面
This is the page on the input class.
这是input类的页面
We use this class to read the axis setup in the input manager
我们通过这个类来读取设置在输入管理器里的轴
and to access multitouch touch and accelerometer data on mobile devices.
以及管理移动设备上的多点触控和加速器数据
We read the text on the top of the page
通过阅读页面顶部的内容
to understand how to use the class.
我们知道如何使用这个类
In our case, to get input from the player on all platforms,
在本例中 我们可以从包括移动端在内的所有平台
including mobile devices.
获得input类
Under the description is a list of class variables and class functions.
描述下方是一个显示类变量和类函数的列表
The class variables hold information
类变量存有信息
like the number of touches in touchCount
比如:“touchCount” 里有触摸帧的数量
or a reference to the default gyroscope with gyro.
“gyro” 就是一个对默认陀螺仪的引用
The class functions do something for us.
类函数能够帮到我们
In our code we will be using Input. GetAxis.
我们的代码就需要用到Input.GetAxis函数
When we find the item we want, we click on the link
找到我们想要的函数 点击链接
to bring up a page on the function or the variable.
打开函数或变量的页面
Let’s look at Input.GetAxis.
我们来看看Input.GetAxis页面
This page includes the signature of the function,
这个页面包含了函数签名
a description of the function
函数描述
and code snippets showing how to use it
以及示例代码片段
in Unity’s Javascript and C#.
在Unity的两种语言JavaScript和C#间
We will be using C#.
我们要使用的是C#
For more information on the input manager and Input.GetAxis
想要了解更多有关输入管理器和 Input.GetAxis的信息
see the lessons linked below.
参见下方链接的课程
We will be using Input.GetAxis
我们对Input.GetAxis的用法
in a very similar way to the code snippet.
与示例代码片段相似
Let’s return to our script and write our code:
让我们回到脚本编写代码:
float moveHorizontal = Input. GetAxis (“horizontal”)
float moveHorizontal = Input. GetAxis (“horizontal”)
float moveVertical = Input. GetAxis (“vertical”)
float moveVertical = Input. GetAxis (“vertical”)
This grabs the input from our player through the keyboard.
这样就能从键盘读取玩家的输入
The float variables move horizontal and move vertical
浮点变量水平移动和垂直移动
record the input from the horizontal and vertical axis,
会记录通过键盘按键控制的
which are controlled by the keys on a keyboard.
水平轴和垂直轴的输入
Our player game object uses a rigid body
玩家游戏对象使用刚体组件
and interacts with a physics engine.
与物理引擎相互作用
We will use this input to add forces to the rigid body
我们会使用这些输入给刚体施力
and move the player game object in the scene.
来移动场景中的玩家游戏对象
To know more about how to apply forces to the rigidbody,
为了了解更多关于如何对刚体施力的信息
let’s check the documentation.
我们来查询文档吧
Type Rigidbody in to our script. Now select rigidbody
在脚本中输入 “Rigidbody” 并选中
and hold down the command key on the mac
按下mac系统的Cmd键
or the control key on the PC
或者windows系统的Ctrl键
and type single quote.
然后输入单引号
Again, this brings a page with a search term”Rigidbody”.
同样会转到带有搜索词 “Rigidbody” 的页面
We want to click on Rigidbody.
点击 “Rigidbody”
This brings up the rigidbody page.
就会进入刚体组件的页面
If we want to apply force to our player game object
如果想要对玩家游戏对象施力
we need to do something.
我们需要做些事情
So let’s look at the functions available to the rigidbody class.
那么让我们来看看rigidbody类的可用函数
We read the descriptions until we find the one we want,
阅读描述 直到找到我们想要的那种
in this case let’s choose AddForce.
本例中我们选择AddForce函数
This adds a force to the rigidbody
这个函数可以给刚体添加一个力
as a result the rigidbody will start moving.
使其开始移动
Click the link and we go to the page on AddForce.
点击链接 进入AddForce页面
We can see the signature of the function at the top of the page.
我们能看到页面顶部函数的签名
This signature tells us we need a vector3
签名显示我们需要一个vector3类型和
and an optional ForceMode
一个可选的ForceMode类型的参数
to add force to our rigidbody.
来给给刚体施力
What is a vector3?
什么是vector3?
For more detailed information on vector3
关于vector3更详细的信息
Please see the information linked below.
请参见下方链接中的信息
But in simple terms the vector3 holds 3 decimal values in one container.
简单地来说 vector3就是在一个容器中保存三个十进制值
This makes it easy for us to move around
这让我们能很容易地移动小球
and use values for things like a force in 3D space,
并且用数值来表示三维空间中的力
which requires a value for force on each of the X, Y and Z axis.
要求在X Y Z每个轴上都有一个力的值
Or to describe a rotation
或者用来描述旋转
which would also require a value for each of the X, Y and Z axis.
同样需要为每个X Y Z轴指定一个值
All of our documentation pages are linked together.
所有的文档页面都链接在一起
If we were to click on Vector3,
如果我们点击Vector3的链接
we would be taken to the documentation page for vector3.
就会进入三维向量的文档页面
The same is true for ForceMode, MonoBehaviour and RigidBody.
ForceMode MonoBehaviour和 RigidBody也是如此
Below the description are snippets
在描述的下方
that show us how this function could be used.
我们能看到相应的演示代码片段
Note the second signature for AddForce below the first.
注意AddForce函数第一个签名下面的第二个签名
The descriptions are the same.
描述是一样的
But in this case we can use AddForce
但这种情况下我们可以用
with either a vector3 or 3 float values
带有一个三维向量或者三个浮点值的
for X, Y and Z.
Addforce函数表示X Y Z轴
The next concept that we need to cover is how to get a hold of,
我们需要讨论的下一个概念是如何获得
or how to reference different components on our game object.
或者说如何在游戏对象上引用不同的组件
We are writing a script called PlayerController.
我们正在编写一个名为PlayerController的脚本
Which is attached as a script component to our Player game object.
作为脚本组件附加到玩家游戏对象上
From this script we need to add force using the rigidbody component.
在这个脚本中 我们需要通过刚体组件施力
We want to access that component from this script.
我们想要通过这个脚本访问该组件
There are several ways that we can do this.
有几种方法可以做到
But in our case we will cover only one of the main ways
但在这里 我们只会用到一个最常用的方法
of accessing another component on the same game object.
访问同一游戏对象上的另一个组件
We will create a variable
我们会在脚本中创建一个变量
to hold this reference in our script
用来保存该引用
and we will set this reference in the Start function.
并将引用设置在Start函数中
We see this here in the code snippet.
这个我们在代码片段中可以看到
public Rigidbody rb creates a public variable
代码 “public Rigidbody rb” 创建了一个
with the type of rigidbody called rb
带有刚体类型 “rb” 的公共变量
to hold the reference to the rigidbody we want to access.
来保存对我们想要访问的刚体的引用
In Start the reference is set by using the code
在Start函数中 通过使用代码
GetComponent
GetComponent<Rigidbody> 来设置引用
This will find and return a reference to the attached rigidbody,
找到并返回对所附刚体的引用
if there is one.
前提是这个引用存在的话
All of the code in the start function is called
Start函数中的所有代码
on the first frame that the script is active.
都是在脚本被激活的第一帧被调用
This is often the very first frame of the game.
这通常是游戏的第一帧
Finally in FixedUpdate the attached rigidbody component
最后在FixedUpdate中 通过代码 “rb.AddForce” 写出的变量 “rb”
is accessed through the variable named rb with rb. AddForce.
访问附加的刚体组件
So in our script we need to write
所以在我们脚本中 我们要写
private Rigidbody rb to create the variable to hold the reference.
“private Rigidbody rb” 来创建保存引用的变量
And in a new start function we need to write
然后在一个新的Start函数中 我们要写
rb=GetComponent
rb=GetComponent
In FixedUpdate let’s use the simplest version of Rigidbody. AddForce.
在FixedUpdate函数中我们用最简单的Rigidbody.AddForce
One that simply uses a vector3
只需要用到一个三维向量
and we will leave the ForceMode at default
在代码中省略ForceMode
by omitting it from our code.
使其处于默认状态
So in our script we need to type rb.AddForce
所以在脚本中 我们需要输入 rb.AddForce
and then some vector3 value.
然后是一些三维向量值
Now how do we get our two float values
现在我们要如何将两个浮点值
in to a vector3 value?
转换为三维向量值呢?
Let’s create a new vector3 variable called Movement.
首先创建一个新的三维向量变量 命名movement
This will be equal to a new vector3
等于由xyz轴组成的
that is made up of an X, a Y and a Z.
新的三维向量
The X, Y, Z values will determine the direction of the force
这些XYZ值将决定
we will add to our ball.
我们施加给小球的力的方向
What is our X value?
我们的X值是什么?
That would be moveHorizontal.
就是水平移动的距离
With this our left and right keys will add force
通过它 我们的左右键会给小球施力
moving the ball to the left or right.
控制小球左右移动
What is our Y? 0.
Y轴呢?是0
We don’t want to move up at all.
我们并不需要小球向上移动
What is our Z value? That would be moveVertical.
Z值呢? 那就是垂直移动的距离
Now we use Movement, a vector3 value, in rb. AddForce
现在调用Movement和一个三维向量值到rb. AddForce中
as rb. AddForce(movement).
即rb.AddForce(movement)
Let’s save this script and return to Unity.
保存脚本 回到Unity
We check for errors in our footer or the console
检查页脚或控制台有没有报错
and there are none. Good.
没有错误 很好
Let’s test what we’ve written.
让我们进行测试 查看脚本效果
Hit Play, and
点击播放按钮
by using the keys setup on the input manager
通过控制输入管理器上的设置的按键
the ball moves in the scene.
小球在场景中动了起来
But it’s very slow.
但是非常缓慢
This is too slow to be playable,
虽然小球速度太慢玩不起来
but the basic concept works 100%.
但是基本概念是百分之百有效的
To stop testing leave play mode.
停止测试 退出播放模式
Let’s return to our code and create a tool
让我们回到代码那里 创建一个小工具
that will give us control over the speed of the ball.
能够让我们控制小球的速度
We need to multiply our movement by some value.
我们需要将movement乘以某个数值
We could simply enter that value here on our script
我们可以在脚本中简单地键入这个数值
but if we ever needed to make any tweaks or changes
但如果我们需要做任何调整或改动
we would have to return to our scripting editor
就必须回到脚本编辑器
and change the value in the script and then recompile.
更改数值 再重新编译
This takes time.
这太费时间了
The solution is to create a public variable in our script.
解决这一问题的方法是在脚本中创建一个公共变量
Let’s create a public float called Speed.
创建一个名为Speed的公共浮点型变量
By creating a public variable in our script
通过在脚本中创建一个公共变量
this variable will show up in the Inspector as an editable property.
这个变量将在属性面板中显示为可编辑
When we use a public variable
当我们使用公共变量时
we can make all of our changes in the editor.
就可以直接在编辑器中进行所有修改了
We then multiply Movement by Speed.
然后我们用Movement乘以Speed
We now have control over our movement value from inside the editor.
现在我们可以从编辑器内部控制移动值
Let’s save these changes and return to Unity.
保存修改并返回Unity
When we return to the editor
当我们返回编辑器
we can see our PlayerController script now has a speed property.
可以看到PlayerController脚本现在出现了一个Speed属性
Let’s set this property to 100.
我们把属性值设为100
Now enter play mode.
现在进入播放模式
Whoa! Way too fast!
哇!这太快了!
But changes are fast too.
但是调整也同样很快
Exit play mode and change the value to 10.
退出播放模式 将Speed的值改为10
Hit Play.
点击播放
That’s a little better.
这样好多了
Congratulations, we can now move our character.
太棒了 我们现在可以移动角色了
In the next assignment we’ll talk about how to move the camera.
下一个任务 我们将谈论关于摄像机的移动

发表评论

译制信息
视频概述

unity教程之角色移动,使用c#和unity引擎制作一个移动的小球

听录译者

收集自网络

翻译译者

HiyaTay

审核员

审核员1024

视频来源

https://www.youtube.com/watch?v=7C7WWxUxPZE

相关推荐