[unity3d]鼠标拖动and旋转缩放

using UnityEngine;
using System.Collections;

public class mouseRotateAndScale : MonoBehaviour {
	GameObject car;
	//public GameObject light;
	Vector2 p1, p2;//用来记录鼠标的位置,以便计算旋转幅度
	 // 鼠标中间键
    int MouseWheelSensitivity = 5;
    int MouseZoomMin = 18;
    int MouseZoomMax = 90;
    float normalDistance = 60;
	// Use this for initialization
	void Start () {
		car = GameObject.Find("benchi");
		
	}
	
	// Update is called once per frame
	void Update () {
		 //旋转
        if (Input.GetMouseButtonDown(0))
        {
            p1 = new Vector2(Input.mousePosition.x, Input.mousePosition.y);//鼠标左键按下时记录鼠标位置p1
            
        }
        if (Input.GetMouseButton(0))
        {
            p2 = new Vector2(Input.mousePosition.x, Input.mousePosition.y);//鼠标左键拖动时记录鼠标位置p2
            if (transform.position.y >= 20 && transform.position.y <= 130)  //控制在20-130之内
            {  
                float dx = p2.x - p1.x;
                
                float dy = (float)0.6 * (p2.y - p1.y);
                //鼠标上下移动
                transform.Translate(-dy * Vector3.up * Time.deltaTime);
                transform.RotateAround(car.transform.position, Vector3.up, dx * Time.deltaTime);
            }
            else if (transform.position.y < 20 && p2.y < p1.y)
            {
                float dx = p2.x - p1.x;

                float dy = (float)0.6 * (p2.y - p1.y);
                //鼠标上下移动
                transform.Translate(-dy * Vector3.up * Time.deltaTime);
                transform.RotateAround(car.transform.position, Vector3.up, dx * Time.deltaTime);
            }
            else if (transform.position.y > 130 && p2.y > p1.y)
            {
                float dx = p2.x - p1.x;

                float dy = (float)0.6 * (p2.y - p1.y);
                //鼠标上下移动
                transform.Translate(-dy * Vector3.up * Time.deltaTime);
                transform.RotateAround(car.transform.position, Vector3.up, dx * Time.deltaTime);
            }
            //下面开始旋转,仅在水平方向上进行旋转
            
            
            
            //else if (transform.position.y < 20)
            //{
            //    transform.position.y = 20.1f;
            //}
            //else if (transform.position.y > 130)
            //{
            //    transform.position.y = 129.9f;
            //}
            //light.transform.RotateAround(car.transform.position, Vector3.up, dx * Time.deltaTime);
        }


        //鼠标滚轮控制场景大小
        // 如果按住滑轮
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            Debug.Log(1);
            Debug.Log(Input.GetAxis("Mouse ScrollWheel"));
           
            if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax)
            {
                normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
            }

            if (normalDistance < MouseZoomMin)
            {
                normalDistance = MouseZoomMin;
            }
            if (normalDistance > MouseZoomMax)
            {
                normalDistance = MouseZoomMax;
            }
           // transform.Translate(transform.forward * normalDistance);
            transform.camera.fieldOfView = normalDistance;

            
        }

        //后滚
        else if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            Debug.Log(-1);
            if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax)
            {
                normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
            }

            if (normalDistance < MouseZoomMin)
            {
                normalDistance = MouseZoomMin;
            }
            if (normalDistance > MouseZoomMax)
            {
                normalDistance = MouseZoomMax;
            }
           // transform.Translate(-transform.forward * normalDistance);
            transform.camera.fieldOfView = normalDistance;
        }
	}
}

你可能感兴趣的:([unity3d]鼠标拖动and旋转缩放)