生活随笔
收集整理的這篇文章主要介紹了
Unity 3D鼠标操作实现旋转、缩放观察物体
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.實現(xiàn)思路:操作攝像頭圍繞物體中心點(遠點),做球面旋轉(zhuǎn)移動來實現(xiàn)旋轉(zhuǎn)觀察物體,控制球的半徑大小來實現(xiàn)物體的縮放效果。
2.C#攝像頭源碼腳本
using UnityEngine;
public class BaseCam : MonoBehaviour
{
public Vector3 mousePos1;
public Vector3 mousePos2;
public Quaternion start_qua;
public Vector3 start_pos;
void Start(){start_qua = transform.rotation;start_pos = transform.position;}
void Update(){mousePos2 = Input.mousePosition; }
void OnGUI(){
if (Input.GetMouseButton(
0)){mousePos1 = Input.mousePosition; Vector3 offset = mousePos1 - mousePos2;
if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)){transform.RotateAround(
new Vector3(
0,
0,
0), Vector3.up, Time.deltaTime * offset.x *
2f); }
else{transform.RotateAround(
new Vector3(
0,
0,
0), transform.right, -Time.deltaTime * offset.y *
2f);}Debug.Log(
"pos: " + transform.right);}
if (Input.GetMouseButton(
2)){transform.rotation = start_qua;transform.position = start_pos;}
if (Input.GetAxis(
"Mouse ScrollWheel") >
0f && Vector3.Distance(transform.position,
new Vector3(
0,
0,
0)) >
2f){transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis(
"Mouse ScrollWheel") *
5f);}
if (Input.GetAxis(
"Mouse ScrollWheel") <
0f && Vector3.Distance(transform.position,
new Vector3(
0,
0,
0)) <
5f){transform.Translate(Vector3.back * Time.deltaTime * (-Input.GetAxis(
"Mouse ScrollWheel")) *
5f);}}
}
總結(jié)
以上是生活随笔為你收集整理的Unity 3D鼠标操作实现旋转、缩放观察物体的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。