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

Unity 5 官方教程 #3 – 移动摄像机 – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

Unity 5 官方教程 #3 - 移动摄像机

Unity 5 - Roll a Ball game - 3 of 8: Moving the Camera - Unity Official Tutorials

The Camera doesn’t move
由于摄像机不能移动
and from its current position cannot see very much.
以及它所处位置的视野有限
We need to tie the Camera to the Player gameobject.
因此我们需要把GameObject和摄像机绑定起来
First let’s set the position of the Camera.
先来设置摄像机的位置
Let’s lift it up by 10 units
把它升高10个单位
and tilt it down by about 45 degrees.
倾斜大约45度
Next let’s make the camera
然后把摄像机设置为
a child of the Player gameobject.
GameObject的子对象
This is a typical third-person setup.
这是一个典型的第三视角设置
With the Camera as a child of the Player,
由于摄像机是玩家的子对象
when we move the Player’s position
当我们移动玩家的位置时
the Camera moves with it.
摄像机也会随之移动
When the Player rotates the Camera rotates as well.
当玩家旋转时 摄像机也随之旋转
Let’s look at this from a position
我们看向这个
where we can see both the Player and the Came gameobject.
能同时看到玩家和摄像机的位置
Move the Player,
移动玩家
rotate the Player,
转动玩家
the child Camera moves with it.
子对象摄像机也随之移动
Now let’s reset the Player and test.
现在我们重置玩家来进行测试
We enter Play mode, hold down the up arrow to move,
进入游戏模式 按住向上键来移动
Whoah! What’s happening here?
哇哦!发生了什么?
Okay, well as the Camera is a child of the Player’s Sphere,
摄像机作为玩家的附属部分
even though the Camera is not moving at all
尽管摄像机相对于
relative to the Player’s gameobject,
玩家的GameObject没有移动
the Player gameobject is rotating like crazy
但玩家的GameObject却在疯狂滚动
so the Camera’s point of view rotates with it.
所以摄像机的视角也随之滚动
Let’s exit Play mode.
我们退出游戏模式
Unlike a normal third-person game,
和普通的第三视角游戏不同
our Player gameobject is rotating on all 3 axes
我们玩家的GameObject是同时在3个坐标轴上旋转
not just 1.
而不是一个
In a typical third-person setup
在典型的第三视角设置中
the Camera as a child of the Player gameobject
摄像机作为玩家的GameObject的子对象
will always be in a position relative to its immediate parent.
相对于其父对象通常处于一个位置
and this position will be the parent’s position in the game
这个位置是由游戏中父对象的位置决定
modified or offset by any values in the child’s Transform.
但会因为子对象任何值的转变而修改或偏移
We can’t have the Camera as a child of the Player,
因此摄像机不能设置为玩家的子对象
so let’s detach it.
我们把他们拆分开
Our offset value will be the difference
这里的偏移量是
between the Player gameobject and the Camera.
玩家的GameObject和摄像机间的差值
Now we need to associate the Camera with
现在我们需要将摄影机和
the Player gameobject,
玩家的GameObject关联起来
not as a child,
不是作为子对象
but with a script.
而是通过脚本关联
Using the Add Component button, choose New Script.
使用“添加组件”按钮 选择新脚本
We are writing in C#,
用C#来编写
and name the script “CameraController”
把脚本命名为“CameraController”
and then click on Create and Add,
点击 创建并添加
or simply hit the Return or Enter key to confirm our selection.
或者直接按回车键确定
We should note, this way of creating a script will create
我们应该知道 这样创建的脚本
that Script Asset on the root
会在根目录或者项目视图的顶部
or top level of our Project view.
生成脚本资源
File CameraController in the Scripts folder
把CameraController放入脚本文件夹中
and open it for editing.
然后打开它 进行编辑
We need 2 variables here:
这里需要2个变量
A public gameobject reference to the Player
一个玩家GameObject的公有引用
and a private Vector3
和一个私有Vector3类型变量
to hold our offset value.
用来存放我们的偏移量
Offset is private
偏移量是私有变量
because we can set that value here in the script.
因为我们可以在脚本中修改它的值
For our offset value
对于偏移量
we will take the current Transform position of the Camera,
我们用摄像机位置的变化值
and subtract the Transform position of the Player
减去玩家位置的变化值
to find the difference between the two.
得到两者间的差值
So in Start()
在Start方法中
we can make offset equal to our
让偏移量等于
“transform.position – player.transform.position”
“transform.position – player.transform.position”
And then every frame we set our
然后每一帧我们都将
“transform.position” to our
“transform.position”设置为
“player.transform.position + offset”
“player.transform.position + offset”
This means as we move our player with the controls on the keyboard
这意味着 当我们通过键盘控制玩家移动时
that each frame before displaying what the Camera can see,
在显示摄像机镜头的每一帧之前
the Camera is moved into a new position aligned with the Player object.
摄像机会跟随玩家移动到对应的新位置上
Just as if it were a child of that object
和它作为玩家的一个子对象时一样
if it were not rolling around the game board.
但玩家在游戏界面上滚动时则不同
However, Update() is not the best place for this code.
然而 Update() 不是最适合这组代码的方式
It is true that Update() runs every frame,
确实 Update()每一帧都会运行
and in Update() each frame we can track
并且在Update()中 每一帧我们都能追踪
the position of the Player’s gameobject
玩家的GameObject的位置
and set the position of the Camera.
从而设置摄像机的位置
However, for follow cameras, procedural animation
但是 对于追踪摄像机 程序动画
and gathering last known states,
和收集最后已知状态
it’s best to use LateUpdate().
最好使用LateUpdate()
LateUpdate() runs every frame, just like Update().
LateUpdate()和Update()一样每帧都会运行
But it is guaranteed to run
但它只有当
after all items have been processed in Update().
所有对象在Update()处理完成后再运行
So when we set the position of the Camera
所以当我们设置摄像机的位置时
we know absolutely that the Player has moved for that frame.
应该确定玩家在这一帧中已经移动过了
So let’s test this.
让我们来测试下
Let’s save our script and return to Unity.
先保存脚本 然后回到Unity
First we need to create a reference to the Player gameobject
首先 我们需要创建一个玩家GameObject的引用
by dragging the Player gameobject
把玩家的GameObject拖动到
into the Player slot in the CameraController’s component.
CameraController组件的玩家插槽中即可
Enter Play mode.
进入游戏模式
And now we get the behavior we want.
现在 我们得到了我们想要的效果
The Camera follows the rolling ball without rotating…
摄像机跟随着滚动着的球 但自身没有滚动
Even as the ball goes over the edge.
即使球滚出了边缘 也不会动
In the next assignment, we will set up the basic play area
在下一章节中 我们将在指定的游戏区域
and create and place our special pickup objects.
创建并放置特殊的随机物品

发表评论

译制信息
视频概述

如何绑定玩家和摄像机,让它们之间的移动保持一致

听录译者

收集自网络

翻译译者

九月

审核员

审核员1024

视频来源

https://www.youtube.com/watch?v=Xcm5H2J95iI

相关推荐