需要兩個(gè)空物體
將腳本放在空物體a上,空物體b放在a下,相機(jī)放在b下.
————————————————————————————
using UnityEngine;
using System.Collections;
public class FreeCamera : MonoBehaviour
{
??[SerializeField]
??private Transform m_Pivot;
??[SerializeField]
??private Transform m_Camera;
??private float m_TurnSpeed = 1.5f;
??private float m_LookAngle;
??private float m_TiltAngle;
??[SerializeField]
??private float m_TurnSmoothing = 0.0f;???????// How much smoothing to apply to the turn input, to reduce mouse-turn jerkiness
??[SerializeField]
??private float m_TiltMax = 75f;???????????// The maximum value of the x axis rotation of the pivot.
??[SerializeField]
??private float m_TiltMin = 45f;???????????// The minimum value of the x axis rotation of the pivot.
??private Vector3 m_PivotEulers;
??private Quaternion m_PivotTargetRot;
??private Quaternion m_TransformTargetRot;
??private float MoveSpeed = 0;
??// Use this for initialization
??void Start()
??{
??}
??Vector3 dir_z;
??Vector3 dir_x;
??// Update is called once per frame
??void Update()
??{
????if (Input.GetMouseButton(1))
????{
??????FreeCameraRotation();
??????FreeCameraMove();
????}
??}
??void FreeCameraMove()
??{
????dir_z = m_Camera.transform.TransformDirection(new Vector3(0, 0, 1));
????dir_x = m_Camera.transform.TransformDirection(new Vector3(1, 0, 0));
????float move_x = Input.GetAxis("Horizontal");
????float move_z = Input.GetAxis("Vertical");
????if (move_x != 0 || move_z != 0)
????{
??????MoveSpeed = Mathf.Clamp(MoveSpeed += (Time.deltaTime * Time.deltaTime), 0, 10);
??????transform.position += (dir_z * move_z * MoveSpeed);
??????transform.position += (dir_x * move_x * MoveSpeed);
????}
????else
????{
??????MoveSpeed = 0;
????}
??}
??private void FreeCameraRotation()
??{
????if (Time.timeScale < float.Epsilon)
??????return;
????// Read the user input
????float x = Input.GetAxis("Mouse X");
????float y = Input.GetAxis("Mouse Y");
????// Adjust the look angle by an amount proportional to the turn speed and horizontal input.
????m_LookAngle += x * m_TurnSpeed;
????// Rotate the rig (the root object) around Y axis only:
????m_TransformTargetRot = Quaternion.Euler(0f, m_LookAngle, 0f);
????// on platforms with a mouse, we adjust the current angle based on Y mouse input and turn speed
????m_TiltAngle -= y * m_TurnSpeed;
????// and make sure the new value is within the tilt range
????m_TiltAngle = Mathf.Clamp(m_TiltAngle, -m_TiltMin, m_TiltMax);
????// Tilt input around X is applied to the pivot (the child of this object)
????m_PivotTargetRot = Quaternion.Euler(m_TiltAngle, m_PivotEulers.y, m_PivotEulers.z);
????if (m_TurnSmoothing > 0)
????{
??????m_Pivot.localRotation = Quaternion.Slerp(m_Pivot.localRotation, m_PivotTargetRot, m_TurnSmoothing * Time.deltaTime);
??????transform.localRotation = Quaternion.Slerp(transform.localRotation, m_TransformTargetRot, m_TurnSmoothing * Time.deltaTime);
????}
????else
????{
??????m_Pivot.localRotation = m_PivotTargetRot;
??????transform.localRotation = m_TransformTargetRot;
????}
??}
}