将下面的脚本拖到需要旋转的物体上,按住鼠标右键即可旋转物体。
public class RotateObj : MonoBehaviour
{
//旋转最大角度
public int yMinLimit = -20;
public int yMaxLimit = 80;
//旋转速度
public float xSpeed = 25.0f;
public float ySpeed = 12.0f;
//旋转角度
private float x = 0.0f;
private float y = 0.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButton(1))
{
x += -Input.GetAxis("Mouse X")*xSpeed;
y -= -Input.GetAxis("Mouse Y")*ySpeed;
y = ClampAngle(y,yMinLimit,yMaxLimit);
Quaternion rotation = Quaternion.Euler(y,x,0);
transform.rotation = rotation;
}
}
static float ClampAngle(float angle, float min, float max)
{
if(angle<-360)
angle += 360;
if(angle>360)
angle -= 360;
return Mathf.Clamp(angle,min,max);
}
}