Я пытаюсь сделать простую игру о змее о единстве, в которую можно играть на мобильном устройстве. Я дошел до того, что голова змеи способна непрерывно двигаться в направлении, в котором пользователь провел пальцем, но движение головы змеи несколько изменчиво, и мне бы хотелось, чтобы она была более плавной.
У меня есть это в классе publi c:
Vector2 dir = Vector2.right;
Моя функция запуска выглядит так:
void Start() {
dragDistance = Screen.height * 5 / 100; //dragDistance is 5% height of the screen
InvokeRepeating("Move", 0.3f, 0.075f);
}
Внутри моей функции обновления у меня есть это :
if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
{//It's a drag
//check if the drag is vertical or horizontal
if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
{ //If the horizontal movement is greater than the vertical movement...
if ((lp.x > fp.x)) //If the movement was to the right)
{ //Right swipe
dir = Vector2.right;
}
else
{ //Left swipe
dir = -Vector2.right;
}
}
else
{ //the vertical movement is greater than the horizontal movement
if (lp.y > fp.y) //If the movement was up
{ //Up swipe
dir = Vector2.up;
}
else
{ //Down swipe
dir = -Vector2.up;
}
}
}
А после функции обновления у меня есть:
void Move()
{
// Move head into new direction
transform.Translate(dir);
}
Спасибо за вашу помощь.