Untiy开发照相机跟踪视角

照相机看跟踪功能,区分自由视角与缓动视角,视角可旋转、平移、拉远拉近

 /// <summary>
    /// 自由视角
    /// </summary>
    /// <param name="isSlowAction">是否缓动判断</param>
    void FreeAngle(bool isSlowAction) {
        #region 旋转
        if (Input.GetMouseButton(0))
        {
            xCurrentAngle += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
            yCurrentAngle -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
            yCurrentAngle = ClampAngle(yCurrentAngle, yMinLimit, yMaxLimit);//y旋转角度限制
        }

        Quaternion rotationTo = Quaternion.Euler(yCurrentAngle, xCurrentAngle, 0);
        #endregion
        #region 缩放
				if (Input.GetAxis("Mouse ScrollWheel") != 0.0f )
				{
            zCurrentScale = Input.GetAxis("Mouse ScrollWheel") * scaleSpeed;
        }
        #endregion
        #region 平移
        if(Input.GetMouseButton(1)) {
            xCurrentScale = -Input.GetAxis("Mouse X") * Time.deltaTime * xMoveSpeed;
            yCurrentScale = -Input.GetAxis("Mouse Y") * Time.deltaTime * yMoveSpeed;
            xCurrentScale = xCurrentScale > 1 ? 1 : xCurrentScale;
            yCurrentScale = yCurrentScale > 1 ? 1 : yCurrentScale;
            xCurrentScale = xCurrentScale < -1 ? -1 : xCurrentScale;
            yCurrentScale = yCurrentScale < -1 ? -1 : yCurrentScale;
        }
        #endregion
        if(isSlowAction)//带缓动效果
        {
            m_Transform.rotation = Quaternion.Lerp(m_Transform.rotation, rotationTo, Time.deltaTime * lerpSpeed);
            m_Transform.position = Vector3.Lerp(m_Transform.position, m_Transform.position + m_Transform.forward * (zCurrentScale > 0 ? ((zCurrentScale -= Time.deltaTime * slowlySpeed) < 0 ? zCurrentScale = 0 : zCurrentScale) : ((zCurrentScale += Time.deltaTime * slowlySpeed) > 0 ? zCurrentScale = 0 : zCurrentScale)) + m_Transform.right * (xCurrentScale > 0 ? ((xCurrentScale -= Time.deltaTime * slowlySpeed) < 0 ? xCurrentScale = 0 : xCurrentScale) : ((xCurrentScale += Time.deltaTime * slowlySpeed) > 0 ? xCurrentScale = 0 : xCurrentScale)) + m_Transform.up * (yCurrentScale > 0 ? ((yCurrentScale -= Time.deltaTime * slowlySpeed) < 0 ? yCurrentScale = 0 : yCurrentScale) : ((yCurrentScale += Time.deltaTime * slowlySpeed) > 0 ? yCurrentScale = 0 : yCurrentScale)), Time.deltaTime * lerpSpeed);
        }
        else//正常
        {
            Vector3 positionTo = rotationTo * new Vector3(xCurrentScale, yCurrentScale, zCurrentScale) + m_Transform.position;
            xCurrentScale = 0;
            yCurrentScale = 0;
            zCurrentScale = 0;
            m_Transform.rotation = rotationTo;
            m_Transform.position = positionTo;
        }
        m_Transform.position = new Vector3(Mathf.Clamp(m_Transform.position.x, xMinRangeLimit, xMaxRangeLimit), Mathf.Clamp(m_Transform.position.y, yMinRangeLimit, yMaxRangeLimit), Mathf.Clamp(m_Transform.position.z, zMinRangeLimit, zMaxRangeLimit));//自由模式区域限制
    }
    /// <summary>
    /// 跟踪视角
    /// </summary>
    /// <param name="isSlowAction">是否缓动判断</param>
    void FollowAngle(bool isSlowAction) {
#if UNITY_WEBPLAYER
        Debug.Log("web");
        if(Input.GetMouseButton(0)) {
            xCurrentAngle += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
            yCurrentAngle -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
            yCurrentAngle = ClampAngle(yCurrentAngle, yMinLimit, yMaxLimit);//y旋转角度限制
        }
#endif
#if UNITY_STANDALONE_WIN
        //Debug.Log("pc");
        if (Input.GetMouseButton(0))
        {
            xCurrentAngle += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
            yCurrentAngle -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
            yCurrentAngle = ClampAngle(yCurrentAngle, yMinLimit, yMaxLimit);//y旋转角度限制
        }
#endif
				if (Input.GetMouseButton(1) )
				{
            xCurrentScale = -Input.GetAxis("Mouse X") * Time.deltaTime * xMoveSpeed;
            yCurrentScale = -Input.GetAxis("Mouse Y") * Time.deltaTime * yMoveSpeed;
            x += xCurrentScale;
            y += yCurrentScale;
        }
        distance -= Input.GetAxis("Mouse ScrollWheel") * scaleSpeed;
        distance = Mathf.Clamp(distance, minDistanceToTarget, maxDistanceToTarget);//缩放距离限制
        Quaternion rotationTo = Quaternion.Euler(yCurrentAngle, xCurrentAngle, 0);
        Vector3 positionTo = rotationTo * new Vector3(x, y, -distance) + target.position;
        if(isSlowAction)//带缓动效果
        {
            m_Transform.rotation = Quaternion.Lerp(m_Transform.rotation, rotationTo, Time.deltaTime * lerpSpeed);
            m_Transform.position = Vector3.Lerp(m_Transform.position, positionTo, Time.deltaTime * lerpSpeed);
        }
        else//正常
        {
            m_Transform.rotation = rotationTo;
            m_Transform.position = positionTo;
        }
    }
    /// <summary>
    /// 限制视角旋转角度
    /// </summary>
    /// <param name="angle">当前角度</param>
    /// <param name="min">最小旋转角度</param>
    /// <param name="max">最大旋转角度</param>
    /// <returns></returns>
    float ClampAngle(float angle, float min, float max) {
        if(angle < -360)
            angle += 360;
        if(angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小姑娘不爱写代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值