拖动鼠标旋转相机

一个相机旋转脚本,简洁 好用

using UnityEngine;

/// <summary>
/// 主相机脚本 —— 挂载到主相机上,并将 旋转中心节点拖到 pivot 
/// </summary>
public class ChinarCamera : MonoBehaviour
{
    public  Transform pivot;//旋转中心
    public  Vector3   pivotOffset = Vector3.zero;
    public  Transform target;//旋转目标(相机)
    //距离
    public  float     distance       = 10.0f;
    public  float     minDistance    = 2f;
    public  float     maxDistance    = 15f;
    
    public  float     zoomSpeed      = 1f;
    //速度
    public  float     xSpeed         = 250.0f;
    public  float     ySpeed         = 120.0f;
    //仅绕中心y轴旋转
    public  bool      allowYTilt     = true;
    //相机相对角度限制
    public  float     yMinLimit      = -90f;
    public  float     yMaxLimit      = 90f;
    
    private float     x              = 0.0f;
    private float     y              = 0.0f;
    private float     targetX        = 0f;
    private float     targetY        = 0f;
    private float     targetDistance = 0f;
    private float     xVelocity      = 1f;
    private float     yVelocity      = 1f;
    private float     zoomVelocity   = 1f;


    private void Start()
    {
        var angles     = transform.eulerAngles;
        targetX        = x = angles.x;
        targetY        = y = ClampAngle(angles.y, yMinLimit, yMaxLimit);
        targetDistance = distance;
    }


    private void LateUpdate()
    {
        if (!pivot) return;
        var scroll = Input.GetAxis("Mouse ScrollWheel");

        if (scroll > 0.0f) targetDistance -= zoomSpeed;
        else if (scroll < 0.0f)
            targetDistance += zoomSpeed;
        targetDistance     =  Mathf.Clamp(targetDistance, minDistance, maxDistance);
        if (Input.GetMouseButton(1) || (Input.GetMouseButton(0) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))))
        {
            targetX += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            if (allowYTilt)
            {
                targetY -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
                targetY =  ClampAngle(targetY, yMinLimit, yMaxLimit);
            }
        }
        //单指旋转
        //else if(Input.GetTouch(0).phase == TouchPhase.Moved){
		//	targetX += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
        //    if (allowYTilt)
        //    {
        //        targetY -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
        //        targetY =  ClampAngle(targetY, yMinLimit, yMaxLimit);
        //    }
		//}
        x                   = Mathf.SmoothDampAngle(x,              targetX, ref xVelocity, 0.3f);
        y                   = allowYTilt ? Mathf.SmoothDampAngle(y, targetY, ref yVelocity, 0.3f) : targetY;
        Quaternion rotation = Quaternion.Euler(y, x, 0);
        distance            = Mathf.SmoothDamp(distance, targetDistance, ref zoomVelocity, 0.5f);
        Vector3 position    = rotation * new Vector3(0.0f, 0.0f, -distance) + pivot.position + pivotOffset;
        transform.rotation  = rotation;
        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);
    }
}

在这里插入图片描述
参考博文:https://blog.csdn.net/ChinarCSDN/article/details/80862999


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值