unity3d實現相機對場景物體的旋轉、縮放、平移檢視功能、近似於scene下檢視場景的效果
程式碼如下:
using UnityEngine;using System.Collections;public class Camera_control_rotate : MonoBehaviour{public float moveSeed = 80f;//移動速度public float rotateSpeed = 200f;//旋轉速度public float zoomSpeed = 60f;//縮放速度private Transform m_transform;// Use this for initializationvoid Start (){m_transform = gameObject.transform;}// Update is called once per framevoid Update (){if (Input.GetMouseButton (2)) {transform.Translate (Input.GetAxis ("Mouse X") * (-1f) * moveSeed * Time.deltaTime, Input.GetAxis ("Mouse Y") * (-1f) * moveSeed * Time.deltaTime, 0, Space.Self);}transform.Translate (0, 0, Input.GetAxis ("Mouse ScrollWheel") * zoomSpeed * Time.deltaTime, Space.Self);transform.Translate (Input.GetAxis ("Horizontal") * moveSeed * Time.deltaTime, Input.GetAxis ("wnQEdDownQE") * moveSeed * Time.deltaTime, Input.GetAxis ("Vertical") * moveSeed * Time.deltaTime, Space.Self);//定義射線的方向 Vector3 fwd = m_transform.TransformDirection (Vector3.forward);RaycastHit hit;//發射一條射線找到相機圍繞旋轉的點if (Physics.Raycast (m_transform.position, fwd, out hit, 100.0f)) {if (Input.GetMouseButton (0)) {m_transform.RotateAround (hit.point, Vector3.up,Input.GetAxis ("Mouse X") * rotateSpeed * Time.deltaTime);m_transform.RotateAround (hit.point, m_transform.right,Input.GetAxis ("Mouse Y") * -rotateSpeed * Time.deltaTime);}}}}