• #### 科普

SCIENCE

#### 英语

ENGLISH

#### 科技

TECHNOLOGY

MOVIE

FOOD

#### 励志

INSPIRATIONS

#### 社会

SOCIETY

TRAVEL

#### 动物

ANIMALS

KIDS

#### 卡通

CARTOON

#### 计算机

COMPUTER

#### 心理

PSYCHOLOGY

#### 教育

EDUCATION

#### 手工

HANDCRAFTS

#### 趣闻

MYSTERIES

CAREER

GEEKS

#### 时尚

FASHION

• 精品课
• 公开课
• 欢迎下载我们在各应用市场备受好评的APP

点击下载Android最新版本

点击下载iOS最新版本

扫码下载译学馆APP

#### 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.

First let’s set the position of the Camera.

Let’s lift it up by 10 units

and tilt it down by about 45 degrees.

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,

the Player gameobject is rotating like crazy

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

not just 1.

In a typical third-person setup

the Camera as a child of the Player 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.

Now we need to associate the Camera with

the Player gameobject,

not as a child,

but with a script.

Using the Add Component button, choose New Script.

We are writing in C#,

and name the script “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

and open it for editing.

We need 2 variables here:

A public gameobject reference to the Player

and a private 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()

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.

It is true that Update() runs every frame,

and in Update() each frame we can track

the position of the Player’s 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() runs every frame, just like Update().
LateUpdate()和Update()一样每帧都会运行
But it is guaranteed to run

after all items have been processed in 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.

First we need to create a reference to the Player gameobject

by dragging the Player 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.