Unity中的自由相機(jī),在Game模式下,讓相機(jī)像Scene里那樣移動

需要兩個(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;

????}

??}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

友情鏈接更多精彩內(nèi)容