Я создаю базовый симулятор в Unity для своего проекта A-level Computer Science.В настоящий момент пользователь может нарисовать объект в виде ящика (ящика), выбрав соответствующий инструмент, щелкнув и перетащив его, чтобы определить два противоположных угла ящика, определив его размеры.
Коробка состоит из одного префаба, который создается и имеет соответственно измененный размер.Код для него выглядит следующим образом:
void Start () {
boxAnim = boxButton.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//sets the mouseDown and mouseHeld bools and the mouse position Vector3
mouseDown = Input.GetMouseButtonDown(0);
mouseHeld = Input.GetMouseButton(0);
mousePosition = Input.mousePosition;
//checks if the user has started to draw
if (mouseDown && !draw)
{
draw = true;
originalMousePosition = mousePosition;
}
//checking if the user has released the mouse
if (draw && !mouseHeld)
{
finalMousePosition = mousePosition;
draw = false;
if (boxAnim.GetBool("Pressed") == true) //if the box draw button is pressed
{
boxDraw(originalMousePosition, finalMousePosition); //draws crate
}
}
}
void boxDraw(Vector3 start, Vector3 end)
{
//asigns world coordinates for the start and end mouse positions
worldStart = Camera.main.ScreenToWorldPoint(start);
worldEnd = Camera.main.ScreenToWorldPoint(end);
if (worldStart.y >= -3.2f && worldEnd.y >= -3.2f)
{
//determines the size of box to be drawn
boxSize.x = Mathf.Abs(worldStart.x - worldEnd.x);
boxSize.y = Mathf.Abs(worldStart.y - worldEnd.y);
//crate sprite is 175px wide, 175/50 = 3.5 (50px per unit) so the scale factor must be the size, divided by 3.5
boxScaleFactor.x = boxSize.x / 3.5f;
boxScaleFactor.y = boxSize.y / 3.5f;
//initial scale of the box is 1 (this isn't necessary but makes reading program easier)
boxScale.x = 1 * boxScaleFactor.x;
boxScale.y = 1 * boxScaleFactor.y;
//creates a new crate under the name newBox and alters its size
GameObject newBox = Instantiate(box, normalCoords(start, end), box.transform.rotation) as GameObject;
newBox.transform.localScale = boxScale;
}
}
Vector3 normalCoords(Vector3 start, Vector3 end)
{
//takes start and end coordinates as position coordinates and returns a world coordinate coordinate for the box
if(end.x > start.x)
{
start.x = start.x + (Mathf.Abs(start.x - end.x) / 2f);
}
else
{
start.x = start.x - (Mathf.Abs(start.x - end.x) / 2f);
}
if(end.y > start.y)
{
start.y = start.y + (Mathf.Abs(start.y - end.y) / 2f);
}
else
{
start.y = start.y - (Mathf.Abs(start.y - end.y) / 2f);
}
start = Camera.main.ScreenToWorldPoint(new Vector3(start.x, start.y, 0f));
return start;
}
Аналогичным образом я хочу, чтобы можно было создать объект «рампа», чтобы пользователь мог щелкнуть и перетащить, чтобы определитьширину базы, затем щелкните еще раз, чтобы определить угол возвышения / высоты (рампа всегда будет прямоугольным треугольником.) Проблема заключается в том, что я хочу, чтобы рампа была спрайтом, который я создал, а не простоосновной цвет блока.Однако один спрайт будет иметь только один угол возвышения, и никакое преобразование не сможет изменить это (насколько я знаю). Очевидно, я не хочу создавать разные спрайты для каждого угла, поэтомуМогу ли я что-нибудь сделать?
Решение, о котором я думал, было, если бы была какая-то особенность, с помощью которой я мог бы изменить узлы векторного изображения в коде, но я уверен, что этоне существует.
РЕДАКТИРОВАТЬ: просто чтобы прояснить, что это 2D-среда, код включает в себя Vector3s только потому, что это то, что я привык к