- Home /
Question by
Jeeva3m · Oct 19, 2016 at 05:16 AM ·
c#gameobjectanglemouse-dragmousedown
How to set Minimum and Maximum angles on Drag to Rotate Gameobject ??
Hello Friends, I'm trying to create a demo on a product that has to be rotated by mouseClick both x and y but with Minimum and Maximum angles. The following script helps in rotating Gameobject but dont know how to control the rotation angles while mouse dragging. Any help or other ways to do it. (Note: dont want to use camera rotation or orbit).
http://answers.unity3d.com/questions/177391/drag-to-rotate-gameobject.html
(Script by @MattiasWargren)
using UnityEngine;
using System.Collections;
public class ObjectRotator : MonoBehaviour
{
private float _sensitivity;
private Vector3 _mouseReference;
private Vector3 _mouseOffset;
private Vector3 _rotation;
private bool _isRotating;
void Start ()
{
_sensitivity = 0.4f;
_rotation = Vector3.zero;
}
void Update()
{
if(_isRotating)
{
// offset
_mouseOffset = (Input.mousePosition - _mouseReference);
// apply rotation
_rotation.y = -(_mouseOffset.x + _mouseOffset.y) * _sensitivity;
// rotate
transform.Rotate(_rotation);
// store mouse
_mouseReference = Input.mousePosition;
}
}
void OnMouseDown()
{
// rotating flag
_isRotating = true;
// store mouse
_mouseReference = Input.mousePosition;
}
void OnMouseUp()
{
// rotating flag
_isRotating = false;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612094939im_/https://answers.unity.com/themes/thub/images/avi.jpg)