Я практикуюсь в Unity; Я хочу переместить объект вправо и влево в зависимости от того, как я провожу пальцем. На данный момент я получил сценарий, но проблема возникает, когда я его играю. Устанавливает расположение объектов в центре; действия смахивания работают нормально. Однако я не хочу, чтобы объект располагался в центре.
Скрипт:
public class swipeTest : MonoBehaviour {
public SwipeManager swipeControls;
public Transform Player;
private Vector3 desiredPosition;
private void Update() {
if (swipeControls.SwipeLeft)
desiredPosition += Vector3.left;
if (swipeControls.SwipeRight)
desiredPosition += Vector3.right;
Player.transform.position = Vector3.MoveTowards
(Player.transform.position, desiredPosition, 0.5f * Time.deltaTime);
}
}
И еще
public class SwipeManager : MonoBehaviour {
private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
private bool isDraging = false;
private Vector2 startTouch, swipeDelta;
public Vector2 SwipeDelta { get { return swipeDelta; } }
public bool Tap { get { return tap; } }
public bool SwipeLeft { get { return swipeLeft; } }
public bool SwipeRight { get { return swipeRight; } }
public bool SwipeUp { get { return swipeUp; } }
public bool SwipeDown { get { return swipeDown; } }
private void Update() {
tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;
#region Standalone Inputs
if (Input.GetMouseButtonDown(0)) {
tap = true;
isDraging = true;
startTouch = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0)) {
isDraging = false;
Reset();
}
#endregion
#region Mobile Input
if (Input.touches.Length > 0) {
if (Input.touches[0].phase == TouchPhase.Began) {
isDraging = true;
tap = true;
startTouch = Input.touches[0].position;
}
else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled) {
isDraging = false;
Reset();
}
}
#endregion
// Calculate the distance
swipeDelta = Vector2.zero;
if (isDraging) {
if (Input.touches.Length > 0)
swipeDelta = Input.touches[0].position - startTouch;
else if (Input.GetMouseButton(0))
swipeDelta = (Vector2)Input.mousePosition - startTouch;
}
//Did we cross the distance?
if (swipeDelta.magnitude > 125) {
//Which direction?
float x = swipeDelta.x;
float y = swipeDelta.y;
if (Mathf.Abs(x) > Mathf.Abs(y)) {
//Left or right
if (x < 0)
swipeLeft = true;
else
swipeRight = true;
}
else {
// Up or down
if (y < 0)
swipeDown = true;
else
swipeUp = true;
}
Reset();
}
}
void Reset() {
startTouch = swipeDelta = Vector2.zero;
isDraging = false;
}
}
Подробнее здесь: https://stackoverflow.com/questions/494 ... s-in-unity
Управление вводом свайпом в Unity ⇐ C#
Место общения программистов C#
1769238555
Anonymous
Я практикуюсь в Unity; Я хочу переместить объект вправо и влево в зависимости от того, как я провожу пальцем. На данный момент я получил сценарий, но проблема возникает, когда я его играю. Устанавливает расположение объектов в центре; действия смахивания работают нормально. Однако я не хочу, чтобы объект располагался в центре.
[b]Скрипт:[/b]
public class swipeTest : MonoBehaviour {
public SwipeManager swipeControls;
public Transform Player;
private Vector3 desiredPosition;
private void Update() {
if (swipeControls.SwipeLeft)
desiredPosition += Vector3.left;
if (swipeControls.SwipeRight)
desiredPosition += Vector3.right;
Player.transform.position = Vector3.MoveTowards
(Player.transform.position, desiredPosition, 0.5f * Time.deltaTime);
}
}
И еще
public class SwipeManager : MonoBehaviour {
private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
private bool isDraging = false;
private Vector2 startTouch, swipeDelta;
public Vector2 SwipeDelta { get { return swipeDelta; } }
public bool Tap { get { return tap; } }
public bool SwipeLeft { get { return swipeLeft; } }
public bool SwipeRight { get { return swipeRight; } }
public bool SwipeUp { get { return swipeUp; } }
public bool SwipeDown { get { return swipeDown; } }
private void Update() {
tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;
#region Standalone Inputs
if (Input.GetMouseButtonDown(0)) {
tap = true;
isDraging = true;
startTouch = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0)) {
isDraging = false;
Reset();
}
#endregion
#region Mobile Input
if (Input.touches.Length > 0) {
if (Input.touches[0].phase == TouchPhase.Began) {
isDraging = true;
tap = true;
startTouch = Input.touches[0].position;
}
else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled) {
isDraging = false;
Reset();
}
}
#endregion
// Calculate the distance
swipeDelta = Vector2.zero;
if (isDraging) {
if (Input.touches.Length > 0)
swipeDelta = Input.touches[0].position - startTouch;
else if (Input.GetMouseButton(0))
swipeDelta = (Vector2)Input.mousePosition - startTouch;
}
//Did we cross the distance?
if (swipeDelta.magnitude > 125) {
//Which direction?
float x = swipeDelta.x;
float y = swipeDelta.y;
if (Mathf.Abs(x) > Mathf.Abs(y)) {
//Left or right
if (x < 0)
swipeLeft = true;
else
swipeRight = true;
}
else {
// Up or down
if (y < 0)
swipeDown = true;
else
swipeUp = true;
}
Reset();
}
}
void Reset() {
startTouch = swipeDelta = Vector2.zero;
isDraging = false;
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/49489863/controlling-swipe-inputs-in-unity[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия