Объявление
public Vector2(float x, float y);Описание
Создает новый вектор с заданными компонентами x, y.
//Этот скрипт перемещает GameObject в новую позицию x, используя Vector2.
//Прикрепите этот скрипт к GameObject
//Установите позицию x в инспекторе
using UnityEngine;
public class Examples : MonoBehaviour
{
Vector2 m_NewPosition;
// Это новая позиция X. Установите его в Инспекторе.
public float m_XPosition;
// Используйте это для инициализации
void Start()
{
//Initialise the vector
m_NewPosition = new Vector2(0.0f, 0.0f);
}
void Update()
{
//Нажмите клавишу пробела, чтобы изменить компонент x вектора
if (Input.GetKeyDown(KeyCode.Space))
{
m_NewPosition.x = m_XPosition;
}
//Изменяем положение в зависимости от вектора
transform.position = m_NewPosition;
}
}
//Этот скрипт показывает, как GameObject можно перемещать на новые позиции с помощью векторов.
//Прикрепите этот скрипт к GameObject. Щелкните GameObject, и появится окно инспектора. Измените «Второй вектор» в Инспекторе
// Прикрепляем GameObject к полю MyTransform
//Также создайте 3 кнопки пользовательского интерфейса (Создать>Кнопку пользовательского интерфейса>Кнопку) и перейдите к каждому из их инспекторов, чтобы изменить их метод OnClick (нажмите кнопку кнопку +)
//Поместите свой GameObject в каждое поле. Используйте раскрывающееся меню Нет функции, чтобы назначить одну кнопку с методом ZeroButton, одну с методом PositiveButton и другую с методом GameObjectButton.
using UnityEngine;
public class Example : MonoBehaviour
{
//Use these to set the GameObject's position
Vector2 m_MyFirstVector;
//Set this Vector in the Inspector (the position you would like the GameObject to move to)
public Vector2 m_MySecondVector;
Vector2 m_MyThirdVector;
//You must assign to this Transform in the Inspector (could be another GameObject)
public Transform m_MyTransform;
void Start()
{
//Set the first vector to be at (0, 0, 0)
m_MyFirstVector = Vector2.zero;
//Set the third vector to the GameObject you set in the Inspector's position
m_MyThirdVector = m_MyTransform.position;
}
public void ZeroButton()
{
//Press this Button to move your GameObject to the first vector position (0, 0)
//Use this to move your GameObject to the origin (be wary of parent GameObjects)
transform.position = m_MyFirstVector;
}
//Press this Button to move your GameObject to the second vector position (100, 200)
public void PositionButton()
{
//Use this to move your GameObject to a specified position
transform.position = m_MySecondVector;
}
//Press this Button to move your GameObject to the third vector position (The m_MyTransform's position)
public void GameObjectButton()
{
//Use this to move your GameObject to another GameObject's position
transform.position = m_MyThirdVector;
}
}
Скрипт, преобразующий градусы в радианы. Радианы используются для поворота вокруг начала координат.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExampleScene : MonoBehaviour
{
void Start()
{
float degrees = 45.0f;
float radians = degrees * Mathf.Deg2Rad;
Vector2 vec2 = new Vector2(Mathf.Cos(radians), Mathf.Sin(radians));
Debug.Log(vec2);
}
}