Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Optimizing Camera Movement | Programming with C# - Extra Mechanics
Unity for Beginners

bookOptimizing Camera Movement

Свайпніть щоб показати меню

In this lesson, we create a CameraFollow script to control the camera's movement independently from the player.

Instead of making the camera a child of the player (which makes movement feel static), we can program the camera to move only when the player reaches the center of the screen.

How It Works

The script is attached to the Camera.

We create a public Transform variable to reference the player.

Because the player is a different GameObject, we make the variable public so we could assign it in the Inspector.

We use an if statement to check:

If the player's Y position is higher than the camera's Y position -> move the camera to the player.

Important Concept: Transform.position Is a Vector3

You cannot modify transform.position.y directly because:

  • transform.position is a Vector3;
  • a Vector3 contains x, y, z values together;
  • you must assign a new Vector3 to change position.

So instead of:

transform.position.y = target.position.y;

We write:

transform.position = new Vector3(
transform.position.x,
target.position.y,
transform.position.z
);

This keeps the camera's X and Z the same, but updates Y.

1. Why did we declare the player's Transform variable as public?

2. Why can’t you write this line to set the camera’s Y position to the player’s Y position?

3. What does this condition check?

question mark

Why did we declare the player's Transform variable as public?

Виберіть правильну відповідь

question mark

Why can’t you write this line to set the camera’s Y position to the player’s Y position?

Виберіть правильну відповідь

question mark

What does this condition check?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 1

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 4. Розділ 1
some-alt