Making PC game together
Making PC game together

Полная версия

Making PC game together

Язык: Английский
Год издания: 2026
Добавлена:
Настройки чтения
Размер шрифта
Высота строк
Поля
На страницу:
3 из 3

In order for our program, our script, to be able to control the newly created head with eyes, we need to programmatically interact with it, i.e. we need to get a reference to it, a reference to the head object. In general, in Unity3d, everything is an objects and you can get a reference to all of them and, accordingly, control them. So remember that.

First, in the body of the «FPCharacter’ class, we declare a reference variable to the «Transform’ component and 4 number variables:

Transform HeadTransf;

float RotatX;

public float SensVert = 2.0f;

public float MinVert = -70f;

public float MaxVert = 70f;

Now let’s write a line of code to get a reference to an object in the Start () method:

HeadTransf = transform.GetChild (0);

The GetChild () method is needed to create a reference to a child object. When we created the «Head’ object, you saw that it appeared as if under the «Player’ object, i.e. it is its child object, and for the time being it repeats all the movements and rotations of its «parent», all this is a simple hierarchy. Such a hierarchy can be multi-level — a child object, a child object of a child object, etc., and is a fundamental feature of Unity3d, extremely easy to create and edit on the fly, and very useful in almost all types of work in the Unity3d development environment, both programming and artistic.

GetChild (0) means that we get the child object with index 0, i.e. simply the first from top to bottom in the hierarchy. In programming, the counting starts from zero, i.e. if you have 10 objects, then you can get each of them in order starting from 0 and ending with 9. To get used to this way of counting and quickly navigate in it, because in nature this is impossible, in nature nothing can be called a zero number, because if you have 10 apples in your hands, then any first apple is number 1, and the tenth is 10, so, to quickly navigate in such a counting system when you have a specified number of elements or objects, then just subtract one from their total number and there will be no difficulties. If you have 23 apples, then in programming you need to address them as 0—22.

Now, in the Update () method, at the bottom, we write the following lines:

RotatX = RotatX — Input.GetAxis («Mouse Y») * SensVert;

RotatX = Mathf.Clamp (RotatX, MinVert, MaxVert);

HeadTransf. localEulerAngles = new Vector3 (RotatX, HeadTransf. localEulerAngles. y, 0);

The first one reads the mouse movement along the Y axis; the second one limits vertical movements using two variables MinVert and MaxVert, simulating real up-down limitations of a human head; the third line forms the rotation vector of our virtual head, i.e. applies the formed rotation.

The general appearance of our program is now as follows, compare it with your version:

using UnityEngine;

public class FPCharacter: MonoBehaviour

{

public float speed = 2.0f;

public float speedfast = 50.0f;

public float gravity = -9.8f;

public float SensHoriz = 2.0f;

public float SensVert = 2.0f;

public float MinVert = -70f;

public float MaxVert = 70f;


CharacterController CharControl;

float _speedfast;


Transform HeadTransf;

float RotatX;


void Start ()

{

CharControl = GetComponent ();

HeadTransf = transform.GetChild (0);

}


void Update ()

{

if (Input.GetKey (KeyCode. LeftShift))

{

_speedfast = speedfast;

}

else

{

_speedfast = 1;

}


float offsetX = Input.GetAxis («Horizontal») * speed * _speedfast;

float offsetZ = Input.GetAxis («Vertical») * speed * _speedfast;

Vector3 movement = new Vector3 (offsetX, 0, offsetZ);

movement = Vector3.ClampMagnitude (movement, speedfast);

movement. y = gravity;

movement *= Time.deltaTime;

movement = transform.TransformDirection (movement);

CharControl.Move (movement);


transform.Rotate (0, Input.GetAxis («Mouse X») * SensHoriz, 0);


RotatX = RotatX — Input.GetAxis («Mouse Y») * SensVert;

RotatX = Mathf.Clamp (RotatX, MinVert, MaxVert);

HeadTransf. localEulerAngles = new Vector3 (RotatX, HeadTransf. localEulerAngles. y, 0);

Конец ознакомительного фрагмента.

Текст предоставлен ООО «Литрес».

Прочитайте эту книгу целиком, купив полную легальную версию на Литрес.

Безопасно оплатить книгу можно банковской картой Visa, MasterCard, Maestro, со счета мобильного телефона, с платежного терминала, в салоне МТС или Связной, через PayPal, WebMoney, Яндекс.Деньги, QIWI Кошелек, бонусными картами или другим удобным Вам способом.

Конец ознакомительного фрагмента
Купить и скачать всю книгу
На страницу:
3 из 3