120 lines
3.0 KiB
C#
120 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class ModelRotator : MonoBehaviour
|
|
{
|
|
private float _sensitivity;
|
|
private Vector3 _mouseReference;
|
|
private Vector3 _mouseOffset;
|
|
private Vector3 _rotation;
|
|
private bool _isRotating;
|
|
|
|
void Start()
|
|
{
|
|
_sensitivity = 0.4f;
|
|
_rotation = Vector3.zero;
|
|
}
|
|
|
|
[SerializeField]private bool canRotate = true;
|
|
public void EnableRotation(bool canRotate)
|
|
{
|
|
return;
|
|
this.canRotate = canRotate;
|
|
}
|
|
|
|
public Transform rotateTransform;
|
|
|
|
|
|
private Vector2 _touchReference;
|
|
private Vector2 _touchOffset;
|
|
void Update()
|
|
{
|
|
if (!canRotate)
|
|
return;
|
|
/*
|
|
if (Input.touchCount > 0)
|
|
{
|
|
Touch touch = Input.GetTouch(0);
|
|
if (touch.phase == TouchPhase.Began)
|
|
{
|
|
// rotating flag
|
|
_isRotating = true;
|
|
// store mouse
|
|
_touchReference = touch.position;
|
|
}
|
|
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
|
|
{
|
|
// rotating flag
|
|
_isRotating = false;
|
|
}
|
|
else if (touch.phase == TouchPhase.Moved)
|
|
{
|
|
if (_isRotating)
|
|
{
|
|
// offset
|
|
_touchOffset = (touch.position - _touchReference);
|
|
|
|
// apply rotation
|
|
_rotation.y = -(_touchOffset.x + _touchOffset.y) * _sensitivity;
|
|
|
|
// rotate
|
|
rotateTransform.Rotate(_rotation);
|
|
|
|
// store mouse
|
|
_touchReference = touch.position;
|
|
}
|
|
}
|
|
}*/
|
|
|
|
|
|
if (_isRotating)
|
|
{
|
|
// offset
|
|
_mouseOffset = (Input.mousePosition - _mouseReference);
|
|
|
|
/*
|
|
|
|
if(Vector3.Dot(transform.up, Vector3.up) >= 0)
|
|
{
|
|
rotateTransform.Rotate(transform.up, -Vector3.Dot(_mouseOffset, Camera.main.transform.right) * _sensitivity, Space.World);
|
|
}
|
|
else
|
|
{
|
|
rotateTransform.Rotate(transform.up, Vector3.Dot(_mouseOffset, Camera.main.transform.right) * _sensitivity, Space.World);
|
|
}
|
|
|
|
rotateTransform.Rotate(Camera.main.transform.right, Vector3.Dot(_mouseOffset, Camera.main.transform.up) * _sensitivity, Space.World);
|
|
*/
|
|
|
|
// apply rotation
|
|
_rotation.y = -(_mouseOffset.x + _mouseOffset.y) * _sensitivity;
|
|
|
|
|
|
// rotate
|
|
rotateTransform.Rotate(_rotation);
|
|
|
|
// store mouse
|
|
_mouseReference = Input.mousePosition;
|
|
}
|
|
}
|
|
|
|
void OnMouseDown()
|
|
{
|
|
// rotating flag
|
|
_isRotating = true;
|
|
|
|
// store mouse
|
|
_mouseReference = Input.mousePosition;
|
|
}
|
|
|
|
void OnMouseUp()
|
|
{
|
|
// rotating flag
|
|
_isRotating = false;
|
|
}
|
|
|
|
|
|
|
|
}
|