unity中模型根据手指的滑动旋转和缩放
参考网站:https://blog.csdn.net/qq_18995513/article/details/53750536;---https://blog.csdn.net/caojianhua1993/article/details/52040918;---https://www.cnblogs.com/SunBool/articles/4730765.html;
1.鼠标控制模型的旋转:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test1 : MonoBehaviour
{
public GameObject obj;
void Update ()
{
if (Input.GetMouseButton(0))//鼠标左键或者右键
{
//控制上下左右旋转
obj.transform.Rotate(Vector3.up, -Time.deltaTime * 200 * Input.GetAxis("Mouse X"));
obj.transform.Rotate(Vector3.right, Time.deltaTime * 200 * Input.GetAxis("Mouse Y"));
}
}
}
2.手指触碰控制模型旋转和缩放:
using UnityEngine;
using System.Collections;
public class Test1_1 : MonoBehaviour
{
private Touch oldTouch1; //上次触摸点1(手指1)
private Touch oldTouch2; //上次触摸点2(手指2)
private int typeRoration = 0;//记录旋转的种类
void Update()
{
//没有触摸,就是触摸点为0
if (Input.touchCount <= 0)
{
return;
}
//单点触摸
if (1 == Input.touchCount)
{
Touch touch = Input.GetTouch(0);
Vector2 deltaPos = touch.deltaPosition;
if(typeRoration == 0)//自由旋转
{
transform.Rotate(Vector3.down * deltaPos.x, Space.World);//绕Y轴进行旋转
transform.Rotate(Vector3.right * deltaPos.y, Space.World);//绕X轴进行旋转,下面我们还可以写绕Z轴进行旋转
}
if (typeRoration == 1)//水平旋转
{
transform.Rotate(Vector3.down * deltaPos.x, Space.World);//绕Y轴进行旋转
//transform.Rotate(Vector3.right * deltaPos.y, Space.World);//绕X轴进行旋转,下面我们还可以写绕Z轴进行旋转
}
if (typeRoration == 2)//上下旋转
{
//transform.Rotate(Vector3.down * deltaPos.x, Space.World);//绕Y轴进行旋转
transform.Rotate(Vector3.right * deltaPos.y, Space.World);//绕X轴进行旋转,下面我们还可以写绕Z轴进行旋转
}
}
//多点触摸, 放大缩小
Touch newTouch1 = Input.GetTouch(0);
Touch newTouch2 = Input.GetTouch(1);
//第2点刚开始接触屏幕, 只记录,不做处理
if (newTouch2.phase == TouchPhase.Began)
{
oldTouch2 = newTouch2;
oldTouch1 = newTouch1;
return;
}
//计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
//两个距离之差,为正表示放大手势, 为负表示缩小手势
float offset = newDistance - oldDistance;
//放大因子, 一个像素按 0.01倍来算(100可调整)
float scaleFactor = offset / 100f;
Vector3 localScale = transform.localScale;
Vector3 scale = new Vector3(localScale.x + scaleFactor,
localScale.y + scaleFactor,
localScale.z + scaleFactor);
//在什么情况下进行缩放
if (scale.x >= 0.05f && scale.y >= 0.05f && scale.z >= 0.05f)
{
transform.localScale = scale;
}
//记住最新的触摸点,下次使用
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}
public void ShiftRorationTypeBtnFunction(GameObject go)//旋转分为三种模式:1.自由旋转;2.水平旋转;3.上下旋转;
{
string name = go.name.ToString();
int index = int.Parse(name.Substring(name.Length - 1, 1));
switch (index)
{
case 0:
{
typeRoration = 0;
this.transform.localEulerAngles = new Vector3(0, 0, 0);//模型角度归零
break;
}
case 1:
{
typeRoration = 1;
this.transform.localEulerAngles = new Vector3(0, 0, 0);
break;
}
case 2:
{
typeRoration = 2;
this.transform.localEulerAngles = new Vector3(0, 0, 0);
break;
}
}
}
}