54 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using UnityEngine;
/**
* 因为物体在不断随着鼠标的运动而旋转,所以旋转时一定要在世界坐标中,否则我们会看到物体绕着自身的旋转轴旋转,这是不对的。
* 另外我们都是通过camera来看到的所以Rotate的第一个参数axis一定要是camera的某个轴向左右方向的旋转需要绕着camera的up方向
* 上下方向的旋转需要绕着camera的right方向。
*/
public class ModelRotate : MonoBehaviour
{
private Vector3 startPos;
private Quaternion startRot;
private Vector3 screenPoint;
private Vector3 offset;
private Camera mCamera;
public void Init(Camera c)
{
startPos = transform.localPosition;
startRot = transform.localRotation;
mCamera = c;
}
private void OnMouseDown()
{
if (mCamera == null) return;
screenPoint = mCamera.WorldToScreenPoint(transform.position);
offset = transform.position - mCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
private void OnMouseDrag()
{
if (mCamera == null) return;
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curWorldPoint = mCamera.ScreenToWorldPoint(curScreenPoint);
transform.position = curWorldPoint + offset;
}
private void Update()
{
RotateXY();
}
private void RotateXY()
{
if (Input.GetMouseButton(1))
{
Vector3 vaxis = Vector3.Cross(Vector3.forward, Vector3.right);
transform.Rotate(vaxis, Input.GetAxis("Mouse X") * 7, Space.World);
Vector3 haxis = Vector3.Cross(Vector3.forward, Vector3.up);
transform.Rotate(haxis, -Input.GetAxis("Mouse Y") * 7, Space.World);
}
}
public void ResetPosAndRot()
{
transform.localPosition = startPos;
transform.localRotation = startRot;
}
}