Drag(拖拽)和Move(移动)两个脚本

时间:2020-12-23 07:07:58

Drag

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Drag : MonoBehaviour { private Transform _trans;// 目标物体的空间变换组件
private Vector3 _vec3TargetScreenSpace;// 目标物体的屏幕空间坐标
private Vector3 _vec3TargetWorldSpace;// 目标物体的世界空间坐标
private Vector3 _vec3MouseScreenSpace;// 鼠标的屏幕空间坐标
private Vector3 _vec3Offset;// 偏移 void Awake() { _trans = transform; }
IEnumerator OnMouseDown()
{
// 把目标物体的世界空间坐标转换到它自身的屏幕空间坐标
_vec3TargetScreenSpace = Camera.main.WorldToScreenPoint(_trans.position);
// 存储鼠标的屏幕空间坐标(Z值使用目标物体的屏幕空间坐标)
_vec3MouseScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _vec3TargetScreenSpace.z);
// 计算目标物体与鼠标物体在世界空间中的偏移量
_vec3Offset = _trans.position - Camera.main.ScreenToWorldPoint(_vec3MouseScreenSpace);
// 鼠标左键按下
while (Input.GetMouseButton(0))
{
// 存储鼠标的屏幕空间坐标(Z值使用目标物体的屏幕空间坐标)
_vec3MouseScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _vec3TargetScreenSpace.z);
// 把鼠标的屏幕空间坐标转换到世界空间坐标(Z值使用目标物体的屏幕空间坐标),加上偏移量,以此作为目标物体的世界空间坐标
_vec3TargetWorldSpace = Camera.main.ScreenToWorldPoint(_vec3MouseScreenSpace) + _vec3Offset;
// 更新目标物体的世界空间坐标
_trans.position = _vec3TargetWorldSpace;
// 等待固定更新
yield return new WaitForFixedUpdate();
}
}
}

Move

using UnityEngine;
using System.Collections; public class Move : MonoBehaviour
{
public Transform target;
float distance = 30f;
float xSpeed = 150f;
float ySpeed = 150f;
float yMinLimit = -180f;
float yMaxLimit = 180f;
float x = 0f;
float y = 0f;
Vector2 oldPosition1;
Vector2 oldPosition2;
private bool flag_Roable = true;//自动旋转标志 private System.DateTime oldTime;
private System.DateTime nowTime;
// Use this for initialization
void Start()
{
transform.eulerAngles = new Vector3(0, -90, 0);
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
if (GetComponent<Rigidbody>())
{
GetComponent<Rigidbody>().freezeRotation = true;
}
oldTime = System.DateTime.Now;
} // Update is called once per frame
void Update()
{
nowTime = System.DateTime.Now;
System.TimeSpan ts1 = new System.TimeSpan(oldTime.Ticks);
System.TimeSpan ts2 = new System.TimeSpan(nowTime.Ticks); System.TimeSpan ts = ts2.Subtract(ts1).Duration();
if (ts.Seconds > 8 && !Input.anyKey)
{
flag_Roable = true;
oldTime = System.DateTime.Now;
}
if (Input.anyKey)
{ if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
//x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
//y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
x = Input.GetAxis("Mouse X") * xSpeed ;
y = Input.GetAxis("Mouse Y") * ySpeed ;
transform.Rotate(Vector3.up * -x * Time.deltaTime, Space.Self);
transform.Rotate(Vector3.right * y * Time.deltaTime, Space.Self);
}
}
if (Input.touchCount > 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
{
Vector2 tempPosition1 = Input.GetTouch(0).position;
Vector2 tempPosition2 = Input.GetTouch(1).position;
if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
{
float oldScale = transform.localScale.x;
float newScale = oldScale * 1.025f;
transform.localScale = new Vector3(newScale, newScale, newScale);
}
else
{
float oldScale = transform.localScale.x;
float newScale = oldScale / 1.025f;
transform.localScale = new Vector3(newScale, newScale, newScale); }
//备份上一次触摸点的位置,用于对比
oldPosition1 = tempPosition1;
oldPosition2 = tempPosition2;
}
}
} } bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
{
//函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势
var leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
var leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
if (leng1 < leng2)
{
//放大手势
return true;
}
else
{
//缩小手势
return false;
}
}
// private Touch oldTouch1; //上次触摸点1(手指1)
//private Touch oldTouch2; //上次触摸点2(手指2)
//void Update()
//{
// //没有触摸,就是触摸点为0
// if (Input.touchCount <= 0)
// {
// return;
// } // //多点触摸, 放大缩小
// 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;
//}
}