using UnityEngine; using System.Collections; //应把物机添加Character Controller组件. public class Cubet : MonoBehaviour { //此处定义一个类,所拖动到的物体即是该类的一个对象 public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 } public RotationAxes axes = RotationAxes.MouseXAndY; public float sensitivityX = 15F; public float sensitivityY = 15F; public float minimumX = -360F; public float maximumX = 360F; public float minimumY = -60F; public float maximumY = 60F; float rotationY = 0F; public Transform cam_transform; public Transform per_transform; // Use this for initialization void Start () { per_transform = this.transform; //获取对象的transform,包括有旋转角度,位置等。此处已相当于指针指向了 GameObject cobj = GameObject.Find ("mainCamera"); //获取照相机 cam_transform = cobj.transform; //照相机的transform camStart (); if (GetComponent<Rigidbody>()) //旋转冻结。作用是用于使旋转角度不因物理模拟而改变。 GetComponent<Rigidbody>().freezeRotation = true; } void camStart(){ Vector3 perpos = per_transform.position; perpos.y += 0.5f; perpos.x += 1f; perpos.z += 1f; cam_transform.position = perpos; //设置照相机的位置、角度 cam_transform.eulerAngles = per_transform.eulerAngles; } // Update is called once per frame void Update () { //官方的代码 if (axes == RotationAxes.MouseXAndY) { float rotationX = cam_transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX; rotationY += Input.GetAxis("Mouse Y") * sensitivityY; rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); cam_transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0); } else if (axes == RotationAxes.MouseX) { cam_transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0); } else { rotationY += Input.GetAxis("Mouse Y") * sensitivityY; rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); cam_transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0); } per_transform.localEulerAngles = cam_transform.transform.localEulerAngles; //设置对象的面向始终与照相机的面向保持一致。 } }
时间: 2024-10-05 19:42:20