摄像机仿真组件

/****************************************************
    文件:CameraComponent.cs
	作者:Du.Sir
    邮箱: 2455421531@qq.com
    日期:2020/6/4 4:31:31
	功能:摄像机组件类
*****************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraComponent : View
{
    #region 字段和属性
    public override string Name => Constant.V_CameraComponent;

    //观察目标
    private  Transform Target;

    //观察距离
    public float Distance = 5F;

    //旋转速度
    private float SpeedX = 240;
    private float SpeedY = 120;

    //角度限制
    private float MinLimitY = -180;
    private float MaxLimitY = 180;

    //旋转角度
    private float mX = 0.0F;
    private float mY = 0.0F;

    //鼠标缩放距离最值
    private float MaxDistance = 10;
    private float MinDistance = 1.5F;
    //鼠标缩放速率
    private float ZoomSpeed = 2F;

    //是否启用差值
    public bool isNeedDamping = true;
    //速度
    public float Damping = 10F;

    //存储角度的四元数
    private Quaternion mRotation;

    //屏幕坐标
    private Vector3 mScreenPoint;
    //坐标偏移
    private Vector3 mOffset;

   // public InputDirection m_inputDir = InputDirection.NULL;//手势输入相关
    private Vector3 offset = Vector3.one;//偏移向量

    public float offectSpend = 3f;//便宜速率
    #endregion

    #region Unity回调
    void Start()
    {
        Target = GameObject.FindWithTag("Target").transform;//获取摄像机目标点

        //初始化旋转角度
        mX = transform.eulerAngles.x;
        mY = transform.eulerAngles.y;
    }

    void LateUpdate()
    {
        //鼠标输入旋转
        InputRotate();

        //鼠标滚轮缩放
        InputScale();

        //输入平移
        InputOffset();

        //摄像机位置设置相关
        CameraSetPos();
    }
    #endregion

    #region 方法
    /// <summary>
    /// 角度限制
    /// </summary>
    /// <param name="angle"></param>
    /// <param name="min"></param>
    /// <param name="max"></param>
    /// <returns></returns>
    private 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>
    private void InputRotate()
    {
        //鼠标左键键旋转
        if (Target != null && Input.GetMouseButton((int)MouseButton.MouseButton_Left))
        {
            //获取鼠标输入
            mX += Input.GetAxis("Mouse X") * SpeedX * 0.02F;
            mY -= Input.GetAxis("Mouse Y") * SpeedY * 0.02F;
            //范围限制
            mY = ClampAngle(mY, MinLimitY, MaxLimitY);
            //计算旋转
            mRotation = Quaternion.Euler(mY, mX, 0);

            //根据是否插值采取不同的角度计算方式
            if (isNeedDamping)
            {
                transform.rotation = Quaternion.Lerp(transform.rotation, mRotation, Time.deltaTime * Damping);
            }
            else
            {
                transform.rotation = mRotation;
            }
        }
    }

    /// <summary>
    /// 鼠标输入缩放
    /// </summary>
    private void InputScale()
    {
        Distance -= Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;
        Distance = Mathf.Clamp(Distance, MinDistance, MaxDistance);
    }

    /// <summary>
    /// 摄像机位置设置相关
    /// </summary>
    private void CameraSetPos()
    {
        //重新计算位置
        Vector3 mPosition = mRotation * new Vector3(-1, -1, -Distance) + Target.position + offset;

        //设置相机的位置
        if (isNeedDamping)
        {
            transform.position = Vector3.Lerp(transform.position, mPosition, Time.deltaTime * Damping);
        }
        else
        {
            transform.position = mPosition;
        }
    }

    /// <summary>
    /// 鼠标右键输入偏移
    /// </summary>
    private void InputOffset()
    {
        #region 注释
        手势识别
        //m_inputDir = InputDirection.NULL;
        //if (Input.GetMouseButtonDown((int)MouseButton.MouseButton_Right))
        //{
        //    //   activeInput = true;
        //    m_mousePos = Input.mousePosition;
        //}
        //if (Input.GetMouseButton((int)MouseButton.MouseButton_Right))
        //{
        //    Vector3 Dir = Input.mousePosition - m_mousePos;
        //    if (Dir.magnitude > 20)
        //    {

        //        if (Mathf.Abs(Dir.x) > Mathf.Abs(Dir.y) && Dir.x > 0)
        //        {
        //            m_inputDir = InputDirection.Right;
        //        }
        //        else if (Mathf.Abs(Dir.x) > Mathf.Abs(Dir.y) && Dir.x < 0)
        //        {
        //            m_inputDir = InputDirection.Left;

        //        }
        //        else if (Mathf.Abs(Dir.x) < Mathf.Abs(Dir.y) && Dir.y > 0)
        //        {
        //            m_inputDir = InputDirection.Up;
        //        }

        //        else if (Mathf.Abs(Dir.x) < Mathf.Abs(Dir.y) && Dir.y < 0)
        //        {
        //            m_inputDir = InputDirection.Down;
        //        }
        //    }
        //}

        键盘识别
        //if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.Space))
        //{
        //    m_inputDir = InputDirection.Up;
        //}
        //else if (Input.GetKeyDown(KeyCode.S))
        //{
        //    m_inputDir = InputDirection.Down;
        //}
        //else if (Input.GetKeyDown(KeyCode.A))
        //{
        //    m_inputDir = InputDirection.Left;
        //}
        //else if (Input.GetKeyDown(KeyCode.D))
        //{
        //    m_inputDir = InputDirection.Right;
        //}
        #endregion

        //鼠标偏移
        if (Input.GetMouseButton((int)MouseButton.MouseButton_Right))
        {
            offset -= Vector3.up * Input.GetAxis("Mouse Y") * Time.deltaTime * offectSpend;
            offset -= Vector3.right * Input.GetAxis("Mouse X") * Time.deltaTime * offectSpend;
        }
    }
    #endregion

    #region  回调事件
    /// <summary>
    /// 处理事件
    /// </summary>
    /// <param name="name"></param>
    /// <param name="data"></param>
    public override void HandleEvent(string name, object data)
    {
        throw new System.NotImplementedException();
    }

    /// <summary>
    /// 关心事件
    /// </summary>
    public override void RegisterAttentionEvent()
    {
        base.RegisterAttentionEvent();
    }
    #endregion
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值