JeanShaw的专栏

记录自己开发的心路历程

相机视野控制效果实现

项目中常用到对相机视野里的对象进行旋转,放大等操作,做笔记,上代码

using UnityEngine;

public class CameraController : MonoBehaviour {

    public Transform target;
    public float xSpeed = 200, ySpeed = 200, mSpeed = 2;//x,y,m缩放的速度
    public float xMinLimit = -60, xMaxLimit = 60,yMinLimit = 0, yMaxLimit = 90;
    public float distance = 15, minDistance = 2, maxDistance = 30;
    public bool needDamping = true;//平滑

    [HideInInspector]
    public bool targerFllowMove = true;
    public float downMoveYDistance;//向下平移距离
    public float upMoveYDistance;//向上平移距离
    public float moveXSpeed = 3;//平移x速度
    public float moveYSpeed = 3;//平移y速度
    public float damping = 5.0f;//平滑值

    private float x = 0.0f;
    private float y = 0.5f;
    private Transform targetTemp;
    private float distanceTemp;
    private Vector3 cameraPositionTemp;
    private Vector3 cameraAngleTemp;
    private float xTemp;
    private float yTemp;
    private float xMove;
    private float yMove;
    private Vector3 currentCameraPosition;
    private Vector3 currentTargetPosition;
    private Vector3 panOffset = Vector3.zero;
    private float IdealDistance;

    public void SetTarget(GameObject go)
    {
        target = go.transform;
    }

    public enum State
    {
        Rota,
        MOve,
        Scale,
        None
    }
    public State state = State.Rota;

    void Start()
    {
        IdealDistance = distance;
        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;
        targetTemp = target;
        distanceTemp = distance;
        cameraPositionTemp = transform.position;
        cameraAngleTemp = transform.eulerAngles;
        xTemp = x;
        yTemp = y;
    }

    void LateUpdate()
    {
        if (target)
        {
            if (Input.GetMouseButtonDown(1))
            {
                state = State.Rota;
            }
            if (Input.GetMouseButtonDown(0))
            {

            }
            //缩放
            if (Input.GetAxis("Mouse ScrollWheel") != 0)
            {
                state = State.Scale;
            }

            #region 旋转
            if (state == State.Rota || state == State.Scale)
            {
                if (Input.GetMouseButton(1))
                {
                    x += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
                    y -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
                }
                x = ClampAngle(x, xMinLimit, xMaxLimit);
                y = ClampAngle(y, yMinLimit, yMaxLimit);

                //离目标越近速度越慢
                //distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed * distance*Time.deltaTime;
                distance = Mathf.Lerp(distance, IdealDistance, Time.deltaTime * damping);//平滑距离变化
                IdealDistance -= Input.GetAxis("Mouse ScrollWheel") * distance * mSpeed;//滚动时距离变化

                distance = Mathf.Clamp(distance, minDistance, maxDistance);//限制距离
                IdealDistance = Mathf.Clamp(IdealDistance, minDistance, maxDistance);//限制距离
                Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
                Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
                Vector3 position = rotation * disVector + target.position;
                if (needDamping)
                {
                    transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
                    transform.position = target.position - distance * transform.forward;
                }
                else
                {
                    transform.rotation = rotation;
                    transform.position = position;
                }
            }

        }
            #endregion

        if (Input.GetMouseButtonDown(2))
        {
            state = State.MOve;
            GetCurrentCameraPosition();
        }

        #region  //平移
        if (Input.GetMouseButton(2))
        {
            xMove -= Input.GetAxis("Mouse X") * moveXSpeed * distance * Time.deltaTime;
            yMove -= Input.GetAxis("Mouse Y") * moveYSpeed * distance * Time.deltaTime;
            yMove = Mathf.Clamp(yMove, -downMoveYDistance, upMoveYDistance);

            Vector3 position1 = transform.TransformDirection(xMove, yMove, 0) + currentCameraPosition;

            transform.position = Vector3.Lerp(transform.position, position1, Time.deltaTime * damping);
            if (targerFllowMove)
            {
                target.rotation = transform.rotation;
                Vector3 position = target.TransformDirection(xMove, yMove, 0) + currentTargetPosition;
                target.position = Vector3.Lerp(target.position, position, Time.deltaTime * damping);
            }
        }
        #endregion

    }
    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);
    }
    /// <summary>
    /// 摄像机复位
    /// </summary>
    public void InitCamera()
    {
        transform.position = cameraPositionTemp;
        transform.eulerAngles = cameraAngleTemp;
        x = xTemp;
        y = yTemp;
        target = targetTemp;
        distance = distanceTemp;
    }
    public void ResetCamera()
    {
        state = State.None;
        distance = 1;
    }
    /// <summary>
    /// 获取当前摄像机位置
    /// </summary>
    public void GetCurrentCameraPosition()
    {
        currentCameraPosition = transform.position;
        currentTargetPosition = target.position;
        xMove = 0;
        yMove = 0;
    }

    private Ray ray;
    private RaycastHit hit;

    void FixedUpdate()
    {
        Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit, 500))
        {
            if (Input.GetMouseButtonUp(0))
            {
                GameObject other = hit.collider.gameObject;
                if (other)
                {
                    Debug.Log("Camera");
                }
            }
        }
    }
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/JeanShaw/article/details/49903387
上一篇Unity5.0 AssetBundle 实验
下一篇时间显示功能
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭