標籤:

代碼實現鏡頭跟隨滑鼠移動

//單一職責只負責移動public class PlayerMove : MonoBehaviour { float h,v; public float playerSpeed = 1f; public float playerViewUpDownSpeed = 50f; public float playerViewLeftRightSpeed = 50f; float X; float Y; float angle; int state; // Use this for initialization void Start () { Debug.Log(transform.rotation.y); Debug.Log(Mathf.PI/4); } // Update is called once per frame void Update () { h = Input.GetAxis("Horizontal"); v = Input.GetAxis("Vertical"); X= Input.GetAxis("Mouse X");//獲取滑鼠x軸的值 Y = Input.GetAxis("Mouse Y");//獲取滑鼠x軸的值 // transform.Rotate(playerViewUpDownSpeed * Y * Time.deltaTime, playerViewLeftRightSpeed * X * Time.deltaTime, 0); //鏡頭跟隨滑鼠左右移動 transform.Rotate(0, playerViewLeftRightSpeed * X * Time.deltaTime, 0); //移動限制 if (transform.rotation.y> Mathf.PI / 8) { //強制歸位 transform.rotation = new Quaternion(transform.rotation.x, Mathf.PI / 9, transform.rotation.z, transform.rotation.w); } if ( transform.rotation.y < -Mathf.PI / 8) { transform.rotation = new Quaternion(transform.rotation.x, -Mathf.PI / 8, transform.rotation.z, transform.rotation.w); } //if (Input.GetKey(KeyCode.Z)) //{ // transform.Rotate(20 * Time.deltaTime, 0, 0); //} //if (Input.GetKey(KeyCode.C)) //{ // transform.Rotate(-20 * Time.deltaTime, 0, 0); //} }}

推薦閱讀:

一個簡單的探照燈shader
Unity接入多個SDK的通用介面開發與資源管理(一)
使用頂點投射的方法製作實時陰影
Unity 2D 動態陰影怎麼實現 | Nexus遊戲說

TAG:unity |