I found a script online for rotating an object using mouse speed and its been working great but in one part of my project I need to limit the rotation to about 45 degrees. I’ve searched around and havent been able to get any of the limit rotation scripts I’ve seen to work with what I am currently using. I am pretty new to Unity scripting so any help is greatly appreciated.
Current script:
using UnityEngine;
using System.Collections;
public class MouseOribit: MonoBehaviour {
public float speed = 10.0f;
private Bounds bound;
void Start () {
var collider = gameObject.GetComponent<BoxCollider>();
if(collider ==null)
{
collider = gameObject.AddComponent<BoxCollider>();
Debug.Log("No collider is detected. A new one will be created.");
}
bound = collider.bounds;
Debug.Log ("Collider Size: " + bound.size.ToString());
Debug.Log ("Collider Center: " + bound.center.ToString());
}
void Update () {
if(Input.GetKey(KeyCode.LeftAlt))
{
float speed = 0.0f;
}
else if(Input.GetKey(KeyCode.RightAlt))
{
float speed = 0.0f;
}
else if(Input.GetMouseButton(1))
{
if(bound.size.magnitude > 0)
{
var dtx = Input.GetAxis("Mouse X")*speed;
/*
var dty = Input.GetAxis("Mouse Y")*speed;
*/
var pivot = bound.center;
//Debug.Log(pivot);
//Debug.Log(dtx.ToString() +", " + dty.ToString());
transform.RotateAround(pivot, Vector3.down, dtx);
/* transform.RotateAround(pivot, Vector3.left, dty); */
}
}
}
}