Unity鼠标中键滚动缩放相机,左键点击场景中模型居中显示

using UnityEngine;
using System.Collections;

public class MoustControl : MonoBehaviour
{
    //鼠标右键可以拖拽旋转场景
    //鼠标中键滚动拉伸镜头远近
    private int MouseWheelSensitivity = 10;
    private int maxCamFov = 90;
    private int minCamFov = 10;
    
    public Transform target;
    Transform cam;

    private Vector3 normalized;

    // Use this for initialization
    void Start()
    {
        cam = Camera.main.transform;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform.parent.name == "gobj")
                {
                    print(hit.transform.name);
                    //点击当前对象,将主摄像机的镜头对准该模型,即该模型在摄像机镜头中间位置
                    //targetModel = GameObject.Find("GameObject").transform.FindChild(this.gameObject.name).gameObject;
                    Vector3 relativePos = hit.transform.position - cam.position;
                    print(relativePos.ToString());
                    Quaternion rotation = Quaternion.LookRotation(relativePos);
                    cam.rotation = rotation;
                    //cam.LookAt(hit.transform);
                }
            }
        }

	//滚动鼠标中键滚动缩放相机
        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            float fov = Camera.main.fieldOfView;
            fov += Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
            fov = Mathf.Clamp(fov, minCamFov, maxCamFov);
            Camera.main.fieldOfView = fov;
        }
    }
}

你可能感兴趣的:(Unity3D)