在手指滑动旋转模型的基础上 ,增加点击模型拖拽模型 话不多说 附代码:
using System;
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;
public float RotateSpeed = 300f;
private bool isdrag = false;
Vector3 targetPos;
void Update()
{
movemodel();
rotatemodel();
}
private void movemodel()
{
if (Input.GetMouseButton(0))
{
if (!isdrag)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.name == my_Cube.transform.name)
{
Debug.Log("第一个点点的模型 要拖拽");
isdrag = true;
}
}
}
if (isdrag)
{
Debug.Log("更新模型位置 Input.mousePosition = " + Input.mousePosition.ToString());
targetPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f));
Debug.Log("更新模型位置 targetPos = " + targetPos.ToString());
my_Cube.transform.position = new Vector3(targetPos.x, targetPos.y, 0.1f);
}
}
else {
isdrag = false;
}
}
private void rotatemodel()
{
if (isdrag || Input.touchCount <= 0)
{
return;
}
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
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;
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;
}
else
{
xbackValue = 1;
}
if (nowFingerPos.y - startFingerPos.y > 0)
{
ybackValue = 1;
}
else
{
ybackValue = -1;
}
my_Cube.transform.Rotate((xbackValue * Vector3.up * (xMoveDistance / (xMoveDistance + yMoveDistance))) + (ybackValue * Vector3.right * (yMoveDistance / (xMoveDistance + yMoveDistance)))
* Time.deltaTime * RotateSpeed, Space.World);
}
}
欢迎阅读~~ may the foce be with you
|