Unity3D中運行場景時,實現(xiàn)攝像機的前、后、左、右、上、下,以及鼠標滾輪的放縮,鼠標右鍵的旋轉(zhuǎn)操作。親測有效,可供參考。
按鍵功能介紹:W——前;S——后;A——左;D——右;Q——下降;E——上升;鼠標右鍵——旋轉(zhuǎn);鼠標滾輪——放縮。
Tourcamera腳本需要掛在攝像機組件上。
在攝像機組件中還需要添加“Physics Raycaster”組件。文章來源地址http://www.zghlxwxcb.cn/news/detail-504811.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tourcamera : MonoBehaviour
{
public Transform tourCamera;
#region 相機移動參數(shù)
public float moveSpeed = 1.0f;
public float rotateSpeed = 90.0f;
public float shiftRate = 2.0f;// 按住Shift加速
public float minDistance = 0.5f;// 相機離不可穿過的表面的最小距離(小于等于0時可穿透任何表面)
#endregion
#region 運動速度和其每個方向的速度分量
private Vector3 direction = Vector3.zero;
private Vector3 speedForward;
private Vector3 speedBack;
private Vector3 speedLeft;
private Vector3 speedRight;
private Vector3 speedUp;
private Vector3 speedDown;
#endregion
void Start()
{
if (tourCamera == null) tourCamera = gameObject.transform;
// 防止相機邊緣穿透
//if (tourCamera.GetComponent<Camera>().nearClipPlane > minDistance / 3)
//{
// tourCamera.GetComponent<Camera>().nearClipPlane /= 3;
//}
}
void Update()
{
GetDirection();
// 檢測是否離不可穿透表面過近
RaycastHit hit;
while (Physics.Raycast(tourCamera.position, direction, out hit, minDistance))
{
// 消去垂直于不可穿透表面的運動速度分量
float angel = Vector3.Angle(direction, hit.normal);
float magnitude = Vector3.Magnitude(direction) * Mathf.Cos(Mathf.Deg2Rad * (180 - angel));
direction += hit.normal * magnitude;
}
tourCamera.Translate(direction * moveSpeed * Time.deltaTime, Space.World);
}
private void GetDirection()
{
#region 加速移動
if (Input.GetKeyDown(KeyCode.LeftShift)) moveSpeed *= shiftRate;
if (Input.GetKeyUp(KeyCode.LeftShift)) moveSpeed /= shiftRate;
#endregion
#region 鍵盤移動
// 復位
speedForward = Vector3.zero;
speedBack = Vector3.zero;
speedLeft = Vector3.zero;
speedRight = Vector3.zero;
speedUp = Vector3.zero;
speedDown = Vector3.zero;
// 獲取按鍵輸入
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) speedForward = tourCamera.forward;
if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)) speedBack = -tourCamera.forward;
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) speedLeft = -tourCamera.right;
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) speedRight = tourCamera.right;
if (Input.GetKey(KeyCode.E)) speedUp = Vector3.up;
if (Input.GetKey(KeyCode.Q)) speedDown = Vector3.down;
direction = speedForward + speedBack + speedLeft + speedRight + speedUp + speedDown;
#endregion
#region 鼠標旋轉(zhuǎn)
if (Input.GetMouseButton(1))
{
// 轉(zhuǎn)相機朝向
tourCamera.RotateAround(tourCamera.position, Vector3.up, Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime);
tourCamera.RotateAround(tourCamera.position, tourCamera.right, -Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime);
// 轉(zhuǎn)運動速度方向
direction = V3RotateAround(direction, Vector3.up, Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime);
direction = V3RotateAround(direction, tourCamera.right, -Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime);
}
#endregion
#region 鼠標滾輪效果
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (Camera.main.fieldOfView <= 100)
Camera.main.fieldOfView += 2;
if (Camera.main.orthographicSize <= 20)
Camera.main.orthographicSize += 0.5F;
}
//Zoom in
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (Camera.main.fieldOfView > 2)
Camera.main.fieldOfView -= 2;
if (Camera.main.orthographicSize >= 1)
Camera.main.orthographicSize -= 0.5F;
}
#endregion
}
public Vector3 V3RotateAround(Vector3 source, Vector3 axis, float angle)
{
Quaternion q = Quaternion.AngleAxis(angle, axis);// 旋轉(zhuǎn)系數(shù)
return q * source;// 返回目標點
}
}
文章來源:http://www.zghlxwxcb.cn/news/detail-504811.html
到了這里,關(guān)于Unity3D攝像機,鍵盤控制前后左右上下移動,鼠標控制旋轉(zhuǎn)、放縮的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!