unity摄像机操控功能分享

来源:互联网 发布:c语言类的定义 编辑:程序博客网 时间:2024/05/02 12:18

工作中用到的操控,整理一下进行分享,是修改加copy。。。。自由查看控制功能。

直接上代码:

using UnityEngine;using System.Collections;[AddComponentMenu("Camera-Control/Max Camera v2")]public class MaxCameraV2 : MonoBehaviour{    public Transform target;    public Vector3 targetOffset;    public float distance = 5.0f;    public float maxDistance = 20;    public float minDistance = .6f;    public float xSpeed = 200.0f;    public float ySpeed = 200.0f;    public int yMinLimit = -80;    public int yMaxLimit = 80;    public int zoomRate = 40;    public int ozoomRate = 40;    public float panSpeed = 0.3f;    public float zoomDampening = 5.0f;    public float ozoomDampening = 500.0f;    private float xDeg = 0.0f;    private float yDeg = 0.0f;    private float currentDistance;    private float desiredDistance;    private Quaternion currentRotation;    private Quaternion desiredRotation;    private Quaternion rotation;    private Vector3 position;    void Start() { Init(); }    void OnEnable() { Init(); }    public void Init()    {        //If there is no target, create a temporary target at 'distance' from the cameras current viewpoint        if (!target)        {            GameObject go = new GameObject("Cam Target");            go.transform.position = transform.position + (transform.forward * distance);            target = go.transform;        }        distance = Vector3.Distance(transform.position, target.position);        currentDistance = distance;        desiredDistance = distance;        //be sure to grab the current rotations as starting points.        position = transform.position;        rotation = transform.rotation;        currentRotation = transform.rotation;        desiredRotation = transform.rotation;        xDeg = Vector3.Angle(Vector3.right, transform.right);        yDeg = Vector3.Angle(Vector3.up, transform.up);    }    public float maxView = 3000.0f;    public float minView = 5.0f;    public float movSpeed = 0.1f;    void LateUpdate()    {        if (calcSenCtrl.focusWindiow != -1)        {            return;        }        // If Control and Alt and Middle button? ZOOM!        if (Input.GetMouseButton(2) && Input.GetKey(KeyCode.LeftAlt) && Input.GetKey(KeyCode.LeftControl))        {            desiredDistance -= Input.GetAxis("Mouse Y") * Time.deltaTime * zoomRate * 0.125f * Mathf.Abs(desiredDistance);        }        // If middle mouse and left alt are selected? ORBIT        else if (Input.GetMouseButton(1))        {            xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;            yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;            ////////OrbitAngle            //Clamp the vertical axis for the orbit            yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);            // set camera rotation             desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);            currentRotation = transform.rotation;            rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);            transform.rotation = rotation;        }        // otherwise if middle mouse is selected, we pan by way of transforming the target in screenspace        else if (Input.GetMouseButton(2))        {            //grab the rotation of the camera so we can move in a psuedo local XY space            target.rotation = transform.rotation;            if (Input.GetKey(KeyCode.LeftShift))            {                target.Translate(Vector3.right * -Input.GetAxis("Mouse X") * panSpeed * 10.0f);                target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed * 10.0f, Space.World);            }            else            {                target.Translate(Vector3.right * -Input.GetAxis("Mouse X") * panSpeed);                target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);            }        }        else if (Input.GetKey(KeyCode.W))        {            //grab the rotation of the camera so we can move in a psuedo local XY space            target.rotation = transform.rotation;            target.Translate(Vector3.forward * movSpeed);                       //target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);        }        else if (Input.GetKey(KeyCode.S))        {            //grab the rotation of the camera so we can move in a psuedo local XY space            target.rotation = transform.rotation;            target.Translate(-1.0f * Vector3.forward * movSpeed);            //target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);        }        else if (Input.GetKey(KeyCode.A))        {            //grab the rotation of the camera so we can move in a psuedo local XY space            target.rotation = transform.rotation;            target.Translate(-1.0f * Vector3.right * movSpeed);            //target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);        }        else if (Input.GetKey(KeyCode.D))        {            //grab the rotation of the camera so we can move in a psuedo local XY space            target.rotation = transform.rotation;            target.Translate(1.0f * Vector3.right * movSpeed);            //target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);        }        ////////Orbit Position        if (!camera.isOrthoGraphic)        {            // affect the desired Zoom distance if we roll the scrollwheel            desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);            //clamp the zoom min/max            desiredDistance = Mathf.Clamp(desiredDistance, minView, maxView);            // For smoothing of the zoom, lerp distance            currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);        }        else        {            if (Input.GetKey(KeyCode.LeftControl))            {                // affect the desired Zoom distance if we roll the scrollwheel                desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);                //clamp the zoom min/max                desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);                // For smoothing of the zoom, lerp distance                currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);            }            else            {                float size = camera.orthographicSize;                if (Input.GetKey(KeyCode.LeftShift))                {                    size -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * ozoomRate * 10.0f;                }                else                {                    size -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * ozoomRate;                }                size = Mathf.Clamp(size, minView, maxView);                camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, size, Time.deltaTime * ozoomDampening);            }        }        // calculate position based on the new currentDistance         position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset);        transform.position = position;    }    private static float ClampAngle(float angle, float min, float max)    {        if (angle < -360)            angle += 360;        if (angle > 360)            angle -= 360;        return Mathf.Clamp(angle, min, max);    }//public float orgZdampening  = 5.0f;public void activeWheel(){//zoomDampening = orgZdampening;zoomDampening = 5.0f;}public void deActiveWheel(){//orgZdampening = zoomDampening;zoomDampening = 0.0f;}}


0 0
原创粉丝点击