Совсем новичок в геймдеве?

Посмотрите это пятиминутное видео и узнайте, чего ожидать от первой игры.

Хватит гадать, с чего же начать. В этих уроках вы найдете все, что нужно знать начинающему Unity-игроделу.

PlayerController

This code reflects the current state of the script as of the end of this particular lesson. This script will continue to grow and mature as the series continues.

Collapse view
Expand view
Copy code

Code snippet

using UnityEngine;
              using System.Collections;
              
              public class PlayerController : MonoBehaviour {
              
                  public float speed;
              
                  private Rigidbody rb;
              
                  void Start ()
                  {
                      rb = GetComponent<Rigidbody>();
                  }
              
                  void FixedUpdate ()
                  {
                      float moveHorizontal = Input.GetAxis ("Horizontal");
                      float moveVertical = Input.GetAxis ("Vertical");
              
                      Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
              
                      rb.AddForce (movement * speed);
                  }
              }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

CameraController

Collapse view
Expand view
Copy code

Code snippet

using UnityEngine;
              using System.Collections;
              
              public class CameraController : MonoBehaviour {
              
                  public GameObject player;
              
                  private Vector3 offset;
              
                  void Start ()
                  {
                      offset = transform.position - player.transform.position;
                  }
                  
                  void LateUpdate ()
                  {
                      transform.position = player.transform.position + offset;
                  }
              }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Rotator

Copy code

Code snippet

using UnityEngine;
              using System.Collections;
              
              public class Rotator : MonoBehaviour {
              
                  void Update () 
                  {
                      transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime);
                  }
              }
1
2
3
4
5
6
7
8
9
10

This code reflects the current state of the script as of the end of this particular lesson. This script will continue to grow and mature as the series continues.

PlayerController

Collapse view
Expand view
Copy code

Code snippet

using UnityEngine;
              using System.Collections;
              
              public class PlayerController : MonoBehaviour {
              
                  public float speed;
              
                  private Rigidbody rb;
              
                  void Start ()
                  {
                      rb = GetComponent<Rigidbody>();
                  }
              
                  void FixedUpdate ()
                  {
                      float moveHorizontal = Input.GetAxis ("Horizontal");
                      float moveVertical = Input.GetAxis ("Vertical");
              
                      Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
              
                      rb.AddForce (movement * speed);
                  }
              
                  void OnTriggerEnter(Collider other) 
                  {
                      if (other.gameObject.CompareTag ("Pick Up"))
                      {
                          other.gameObject.SetActive (false);
                      }
                  }
              }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

DisplayManager

Collapse view
Expand view
Copy code

Code snippet

using UnityEngine;
              using UnityEngine.UI;
              using System.Collections;
              
              public class PlayerController : MonoBehaviour {
              
                  public float speed;
                  public Text countText;
                  public Text winText;
              
                  private Rigidbody rb;
                  private int count;
              
                  void Start ()
                  {
                      rb = GetComponent<Rigidbody>();
                      count = 0;
                      SetCountText ();
                      winText.text = "";
                  }
              
                  void FixedUpdate ()
                  {
                      float moveHorizontal = Input.GetAxis ("Horizontal");
                      float moveVertical = Input.GetAxis ("Vertical");
              
                      Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
              
                      rb.AddForce (movement * speed);
                  }
              
                  void OnTriggerEnter(Collider other) 
                  {
                      if (other.gameObject.CompareTag ( "Pick Up"))
                      {
                          other.gameObject.SetActive (false);
                          count = count + 1;
                          SetCountText ();
                      }
                  }
              
                  void SetCountText ()
                  {
                      countText.text = "Count: " + count.ToString ();
                      if (count >= 12)
                      {
                          winText.text = "You Win!";
                      }
                  }
              }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Collapse view
Expand view
Copy code

Code snippet

using UnityEngine;
              using System.Collections;
              
              public class CompletePlayerController : MonoBehaviour {
              
                  public float speed;             //Floating point variable to store the player's movement speed.
              
                  private Rigidbody2D rb2d;       //Store a reference to the Rigidbody2D component required to use 2D Physics.
              
                  // Use this for initialization
                  void Start()
                  {
                      //Get and store a reference to the Rigidbody2D component so that we can access it.
                      rb2d = GetComponent<Rigidbody2D> ();
                  }
              
                  //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
                  void FixedUpdate()
                  {
                      //Store the current horizontal input in the float moveHorizontal.
                      float moveHorizontal = Input.GetAxis ("Horizontal");
              
                      //Store the current vertical input in the float moveVertical.
                      float moveVertical = Input.GetAxis ("Vertical");
              
                      //Use the two store floats to create a new Vector2 variable movement.
                      Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
              
                      //Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
                      rb2d.AddForce (movement * speed);
                  }
              }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Collapse view
Expand view
Copy code

Code snippet

using UnityEngine;
              using System.Collections;
              
              public class CompleteCameraController : MonoBehaviour {
              
                  public GameObject player;       //Public variable to store a reference to the player game object
              
              
                  private Vector3 offset;         //Private variable to store the offset distance between the player and camera
              
                  // Use this for initialization
                  void Start () 
                  {
                      //Calculate and store the offset value by getting the distance between the player's position and camera's position.
                      offset = transform.position - player.transform.position;
                  }
                  
                  // LateUpdate is called after Update each frame
                  void LateUpdate () 
                  {
                      // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
                      transform.position = player.transform.position + offset;
                  }
              }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Copy code

Code snippet

using UnityEngine;
              using System.Collections;
              
              public class CompleteRotator : MonoBehaviour {
              
                  //Update is called every frame
                  void Update () 
                  {
                      //Rotate thet transform of the game object this is attached to by 45 degrees, taking into account the time elapsed since last frame.
                      transform.Rotate (new Vector3 (0, 0, 45) * Time.deltaTime);
                  }
              }
1
2
3
4
5
6
7
8
9
10
11
12
Collapse view
Expand view
Copy code

Code snippet

using UnityEngine;
              using System.Collections;
              
              public class CompletePlayerController : MonoBehaviour {
              
                  public float speed;             //Floating point variable to store the player's movement speed.
                  
                  private Rigidbody2D rb2d;       //Store a reference to the Rigidbody2D component required to use 2D Physics.
                  
              
                  // Use this for initialization
                  void Start()
                  {
                      //Get and store a reference to the Rigidbody2D component so that we can access it.
                      rb2d = GetComponent<Rigidbody2D> ();
                  }
              
                  //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
                  void FixedUpdate()
                  {
                      //Store the current horizontal input in the float moveHorizontal.
                      float moveHorizontal = Input.GetAxis ("Horizontal");
              
                      //Store the current vertical input in the float moveVertical.
                      float moveVertical = Input.GetAxis ("Vertical");
              
                      //Use the two store floats to create a new Vector2 variable movement.
                      Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
              
                      //Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
                      rb2d.AddForce (movement * speed);
                  }
              
                  //OnTriggerEnter2D is called whenever this object overlaps with a trigger collider.
                  void OnTriggerEnter2D(Collider2D other) 
                  {
                      //Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
                      if (other.gameObject.CompareTag("PickUp"))
                              {
                                   other.gameObject.SetActive(false);
                              }
                  }
              }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Collapse view
Expand view
Copy code

Code snippet

using UnityEngine;
              using System.Collections;
              
              //Adding this allows us to access members of the UI namespace including Text.
              using UnityEngine.UI;
              
              public class CompletePlayerController : MonoBehaviour {
              
                  public float speed;             //Floating point variable to store the player's movement speed.
                  public Text countText;          //Store a reference to the UI Text component which will display the number of pickups collected.
                  public Text winText;            //Store a reference to the UI Text component which will display the 'You win' message.
              
                  private Rigidbody2D rb2d;       //Store a reference to the Rigidbody2D component required to use 2D Physics.
                  private int count;              //Integer to store the number of pickups collected so far.
              
                  // Use this for initialization
                  void Start()
                  {
                      //Get and store a reference to the Rigidbody2D component so that we can access it.
                      rb2d = GetComponent<Rigidbody2D> ();
              
                      //Initialize count to zero.
                      count = 0;
              
                      //Initialze winText to a blank string since we haven't won yet at beginning.
                      winText.text = "";
              
                      //Call our SetCountText function which will update the text with the current value for count.
                      SetCountText ();
                  }
              
                  //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
                  void FixedUpdate()
                  {
                      //Store the current horizontal input in the float moveHorizontal.
                      float moveHorizontal = Input.GetAxis ("Horizontal");
              
                      //Store the current vertical input in the float moveVertical.
                      float moveVertical = Input.GetAxis ("Vertical");
              
                      //Use the two store floats to create a new Vector2 variable movement.
                      Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
              
                      //Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
                      rb2d.AddForce (movement * speed);
                  }
              
                  //OnTriggerEnter2D is called whenever this object overlaps with a trigger collider.
                  void OnTriggerEnter2D(Collider2D other) 
                  {
                      //Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
                      if (other.gameObject.CompareTag("PickUp"))
                      
                      //... then set the other object we just collided with to inactive.
                      other.gameObject.SetActive(false);
              
                      //Add one to the current value of our count variable.
                      count = count + 1;
              
                      //Update the currently displayed count by calling the SetCountText function.
                      SetCountText ();
                  }
              
                  //This function updates the text displaying the number of objects we've collected and displays our victory message if we've collected all of them.
                  void SetCountText()
                  {
                      //Set the text property of our our countText object to "Count: " followed by the number stored in our count variable.
                      countText.text = "Count: " + count.ToString ();
              
                      //Check if we've collected all 12 pickups. If we have...
                      if (count >= 12)
                          //... then set the text property of our winText object to "You win!"
                          winText.text = "You win!";
                  }
              }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

Узнайте больше

Изучите ключевые понятия, создав простой аркадный скролл-шутер

Начать

Немного вдохновения

At the end of day you sell content; players don’t care what your engine is called. But to us, Unity was like a secret weapon.

Barry Meade

Commercial Director, Fireproof Games

Within a month, we can have a prototype where the core game mechanics, the way it looks and behaves, the features, and the capabilities of the game we test, are all quite polished.

Ron Rejwan

CTO and Founder, Jelly Button

I know this sounds like a shameless plug, but it’s true: Unity and the Asset Store are the reason I’m able to make games independently.

Kent Hudson

Founder, Orthogonal Games

Вас ждет полуторамиллионное сообщество разработчиков

Документация

Изучите Unity от А до Я

Перейти к документации

FAQ

Выполните поиск в базе знаний Unity или свяжитесь со службой поддержки.

Перейти к базе знаний

Сообщество

Узнавайте новое, делитесь информацией и обсуждайте Unity c опытными пользователями

Перейти к сообществу

Учебный курс

Освойте ключевые навыки и подготовьтесь к сертификации

Перейти к обучающим материалам