其实这个功能很简单,但是经常会用在游戏开发中,比如常见的在角色展示的时候,通过左右滑动去旋转角色观看角色细节这些。
直接上代码:
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0))
{
this.transform.Rotate(Vector3.up, -Input.GetAxis("Mouse X")*10);
//this.transform.Rotate(Vector3.left, -Input.GetAxis("Mouse Y")*10);
}
}
需要注意的是该脚本是挂载到需要旋转的物体上的,其中MouseX是指左右滑动,Y是指上下滑动。Vector3.up和Vector3.left这个代表旋转轴就不用说了吧,可以根据自己项目需求确定旋转轴、旋转速度这些。