基本功能实现:物体通过鼠标左键上下移动,中间键缩放、右键旋转,30秒没操作,物体自动旋转
实例代码:
using UnityEngine; using System.Collections; public class Script_07_11 : MonoBehaviour { public Transform target; private int MouseWheelSensitivity = 50; private int MouseZoomMin = 20; private int MouseZoomMax = 112; private float normalDistance = 500; private bool flag_Roable = true;//自动旋转标志 private Vector3 normalized; private float xSpeed = 250.0f; private float ySpeed = 120.0f; private float x = 0.0f; private float y = 0.0f; private System.DateTime oldTime; private System.DateTime nowTime; void Start () { oldTime = System.DateTime.Now; //transform.LookAt(target); } void Update () { nowTime = System.DateTime .Now; System.TimeSpan ts1 = new System.TimeSpan(oldTime.Ticks); System.TimeSpan ts2 = new System.TimeSpan(nowTime.Ticks); System.TimeSpan ts = ts2.Subtract(ts1).Duration(); if(ts.Seconds>30 && !Input.anyKey) { flag_Roable = true; oldTime = System.DateTime.Now; } if(flag_Roable && Input.anyKey) { flag_Roable = false; } if(flag_Roable)//自动旋转 { x-=Time.deltaTime*30; var rotation = Quaternion.Euler(0,x,0); transform.RotateAround(target.position,Vector3.up,0.3f); } else { if(Input.GetMouseButton(1)) { if(Input.GetAxis("Mouse X")<0) transform.RotateAround(target.position,Vector3.down,4); if(Input.GetAxis("Mouse X")>0) transform.RotateAround(target.position,Vector3.up,4); } else if (Input.GetAxis("Mouse ScrollWheel") != 0) { //normalized = (transform.position - CameraTarget).normalized; if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax) { normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity; } if (normalDistance < MouseZoomMin) { normalDistance = MouseZoomMin; } if (normalDistance > MouseZoomMax) { normalDistance = MouseZoomMax; } //transform.position = normalized * normalDistance; transform.camera.fieldOfView = normalDistance; } else if(Input.GetMouseButton(0)) { //print("wyz-==="+Input.GetAxis("Mouse Y")); if(Input.GetAxis("Mouse Y")<0) //down { Vector3 temp=Vector3.up * 60.0f * Time.deltaTime; print("wyz==up==="+transform.localPosition.y); if(transform.localPosition.y>300) { temp=Vector3.up * 5.0f * Time.deltaTime; transform.Translate(temp); } else { transform.Translate(temp); } } if(Input.GetAxis("Mouse Y")>0) //up { print("wyz==donw==="+transform.localPosition.y); Vector3 temp=Vector3.down * 60.0f * Time.deltaTime; if(transform.localPosition.y<-300) { temp=Vector3.up * 5.0f * Time.deltaTime; transform.Translate(temp); } else { transform.Translate(temp); } } } } } }