Я получил некоторый код отсюда: https://answers.unity.com/questions/34317/rotate-object-with-mouse-cursor-that-slows-down-on.html
private float rotationSpeed = 10.0F;
private float lerpSpeed = 1.0F;
private Vector3 theSpeed;
private Vector3 avgSpeed;
private bool isDragging = false;
private Vector3 targetSpeedX;
void OnMouseDown() {
isDragging = true;
}
void Update() {
if (Input.GetMouseButton(0) && isDragging) {
theSpeed = new Vector3(-Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), 0.0F);
avgSpeed = Vector3.Lerp(avgSpeed, theSpeed, Time.deltaTime * 5);
} else {
if (isDragging) {
theSpeed = avgSpeed;
isDragging = false;
}
float i = Time.deltaTime * lerpSpeed;
theSpeed = Vector3.Lerp(theSpeed, Vector3.zero, i);
}
transform.Rotate(Camera.main.transform.up * theSpeed.x * rotationSpeed, Space.World);
transform.Rotate(Camera.main.transform.right * theSpeed.y * rotationSpeed, Space.World);
}
Этот код вращает объект щелчком мыши и перетаскивает его, и после этого щелчка объект медленно останавливается.Я хочу эту функцию сейчас на мобильном телефоне.Это мой код для мобильной версии:
// Update is called once per frame
public void Update() {
// Track a single touch as a direction control.
if (Input.touchCount > 0) {
Touch touch = Input.GetTouch(0);
// Handle finger movements based on touch phase.
switch (touch.phase) {
// Record initial touch position.
case TouchPhase.Began:
startPos = touch.position;
directionChosen = false;
break;
// Determine direction by comparing the current touch position with the initial one.
case TouchPhase.Moved:
direction = touch.position - startPos;
break;
// Report that a direction has been chosen when the finger is lifted.
case TouchPhase.Ended:
directionChosen = true;
stopSlowly = true;
Debug.Log("end");
break;
}
}
if (directionChosen) {
// Something that uses the chosen direction...
theSpeed = new Vector3(-direction.x, direction.y, 0.0F);
avgSpeed = Vector3.Lerp(avgSpeed, theSpeed, Time.deltaTime * 5);
} else {
if (stopSlowly) {
Debug.Log("TESTOUTPUT");
theSpeed = avgSpeed;
isDragging = false;
}
float i = Time.deltaTime * lerpSpeed;
theSpeed = Vector3.Lerp(theSpeed, Vector3.zero, i);
}
transform.Rotate(camera.transform.up * theSpeed.x * rotationSpeed, Space.World);
transform.Rotate(camera.transform.right * theSpeed.y * rotationSpeed, Space.World);
А вот некоторые переменные, я использую переменные Vector2 для startPos и direction:
private float rotationSpeed = 1f;
private float lerpSpeed = 1.0F;
private Vector3 theSpeed;
private Vector3 avgSpeed;
private bool isDragging = false;
private Vector3 targetSpeedX;
public Vector2 startPos;
public Vector2 direction;
public bool directionChosen;
public bool stopSlowly;
Теперь, если я нажимаю play и поворачиваюОбъект на моем телефоне, он вращается, но не заканчивается сам по себе.Он также вращается очень быстро.Когда я коснулся Объекта один раз, он немедленно останавливается.Пожалуйста, может кто-нибудь сказать мне, что именно не так с моим кодом.Моя цель - просто вращение, инициализируемое от сенсорного ввода и медленного окончания до его остановки.
Спасибо