在网上找了些 只支持x y轴旋转, 不符合预期,所以修改了下,支持任意方向旋转 。记录下
参考链接 点击链接
附代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeviceModelMove : MonoBehaviour
{
private Vector3 startFingerPos;
private Vector3 nowFingerPos;
private float xMoveDistance;
private float yMoveDistance;
private int xbackValue = 0;
private int ybackValue = 0;
public GameObject my_Cube;
// Update is called once per frame
void Update()
{
//没有触摸
if (Input.touchCount <= 0)
{
return;
}
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
//Debug.Log("======开始触摸=====");
startFingerPos = Input.GetTouch(0).position;
}
nowFingerPos = Input.GetTouch(0).position;
if ((Input.GetTouch(0).phase == TouchPhase.Stationary) || (Input.GetTouch(0).phase == TouchPhase.Ended))
{
startFingerPos = nowFingerPos;
//Debug.Log("======释放触摸=====");
return;
}
if (startFingerPos == nowFingerPos)
{
return;
}
xMoveDistance = Mathf.Abs(nowFingerPos.x - startFingerPos.x);
yMoveDistance = Mathf.Abs(nowFingerPos.y - startFingerPos.y);
if (nowFingerPos.x - startFingerPos.x > 0)
{
xbackValue = -1; //沿着X轴负方向移动
}
else
{
xbackValue = 1; //沿着X轴正方向移动
}
if (nowFingerPos.y - startFingerPos.y > 0)
{
ybackValue = 1; //沿着Y轴正方向移动
}
else
{
ybackValue = -1; //沿着Y轴负方向移动
}
my_Cube.transform.Rotate((xbackValue * Vector3.up * (xMoveDistance / (xMoveDistance + yMoveDistance))) + (ybackValue * Vector3.right * (yMoveDistance / (xMoveDistance + yMoveDistance)))
* Time.deltaTime * 200, Space.World);
}
}
欢迎阅读~~
may the foce be with you