多点触控(unity)
using UnityEngine;
using System.Collections;
public class TouchTest : MonoBehaviour
{
public GameObject ball;
private float lastDis = 0;
private float cameraDis = -20;
public float ScaleDump = 0.1f;
void Update()
{
if (Input.touchCount == 1)
{
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Moved)
{
ball.transform.Rotate(Vector3.right, Input.GetAxis("Mouse Y"), Space.World);
ball.transform.Rotate(Vector3.up, -1 * Input.GetAxis("Mouse X"), Space.World);
}
}
else if (Input.touchCount > 1)
{
Touch t1 = Input.GetTouch(0);
Touch t2 = Input.GetTouch(1);
if (t2.phase == TouchPhase.Began)
{
lastDis = Vector2.Distance(t1.position, t2.position);
}
else
if (t1.phase == TouchPhase.Moved && t2.phase == TouchPhase.Moved)
{
float dis = Vector2.Distance(t1.position, t2.position);
if (Mathf.Abs(dis - lastDis) > 1)
{
cameraDis += (dis - lastDis) * ScaleDump;
cameraDis = Mathf.Clamp(cameraDis, -40, -5);
lastDis = dis;
}
}
}
}
private void LateUpdate()
{
this.transform.position = new Vector3(0, 0, cameraDis);
}
private void OnGUI()
{
string s = string.Format("Input.touchCount={0}\ncameraDis=\n{1}",
Input.touchCount, cameraDis);
GUI.TextArea(new Rect(0, 0, Screen.width / 10, Screen.height), s);
if (GUI.Button(new Rect(Screen.width*9/10,0,Screen.width/10,Screen.height/10),"quit"))
{
Debug.Log("quit");
Application.Quit();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下