Unity 摄像机控制脚本(全局通用),可根据需要自己修改!

Unity — 摄像机控制脚本(全局通用),可根据需要自己修改!


本文提供详细教程

记录遇到的难点并帮助同行的朋友们

坚持以最简单的方法传授和把更好的阅读体验带给你们!


一:效果图

在这里插入图片描述



二:脚本!


1:很简单,就不做多介绍了,希望大家多多支持!
2:脚本挂载照相机Main Camera对象上,自己调下参数可以直接用

using UnityEngine;

public class CameraControl : MonoBehaviour
{
    public Transform pivot;                      // 手动添加:被跟踪的对象:pivot——以什么为轴
    public Vector3 pivotOffset = Vector3.zero; // 与目标的偏移量
    public Transform target;                     // 像一个被选中的对象(用于检查cam和target之间的对象)
    public float distance = 10.0f;     // 距目标距离(使用变焦)
    public float minDistance = 2f;        //最小距离
    public float maxDistance = 15f;       //最大距离
    public float zoomSpeed = 1f;        //速度倍率
    public float xSpeed = 250.0f;    //x速度
    public float ySpeed = 120.0f;    //y速度
    public bool allowYTilt = true;      //允许Y轴倾斜
    public float yMinLimit = -90f;      //相机向下最大角度
    public float yMaxLimit = 90f;       //相机向上最大角度
    private float x = 0.0f;      //x变量
    private float y = 0.0f;      //y变量
    private float targetX = 0f;        //目标x
    private float targetY = 0f;        //目标y
    private float targetDistance = 0f;        //目标距离
    private float xVelocity = 1f;        //x速度
    private float yVelocity = 1f;        //y速度
    private float zoomVelocity = 1f;        //速度倍率


    void Start()
    {
        var angles = transform.eulerAngles;                          //当前的欧拉角
        targetX = x = angles.x;                                   //给x,与目标x赋值
        targetY = y = ClampAngle(angles.y, yMinLimit, yMaxLimit); //限定相机的向上,与下之间的值,返回给:y与目标y
        targetDistance = distance;                                       //初始距离数据为10;
    }


    void LateUpdate()
    {
        if (pivot) //如果存在设定的目标
        {
            float scroll = Input.GetAxis("Mouse ScrollWheel"); //获取滚轮轴
            //如果大于0,说明滚动了:那么与目标距离,就减少固定距离1。就是向前滚动,就减少值,致使越来越近
            if (scroll > 0.0f) targetDistance -= zoomSpeed;  
            else if (scroll < 0.0f) targetDistance +=zoomSpeed;        //距离变远             //否则
            targetDistance = Mathf.Clamp(targetDistance, minDistance, maxDistance);      //目标的距离限定在2-15之间
            if (Input.GetMouseButton(1) || Input.GetMouseButton(0) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))) //鼠标右键
            {
                targetX += Input.GetAxis("Mouse X") * xSpeed * 0.02f; //目标的x随着鼠标x移动*5
                if (allowYTilt)                                       //y轴允许倾斜
                {
                    targetY -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; //目标的y随着鼠标y移动*2.4
                    targetY = ClampAngle(targetY, yMinLimit, yMaxLimit); //限制y的移动范围在-90到90之间
                }
            }
            #region 使用了平滑插值
            x = Mathf.SmoothDampAngle(x, targetX, ref xVelocity, 0.3f);  //使用了平滑插值
            if (allowYTilt) y = Mathf.SmoothDampAngle(y, targetY, ref yVelocity, 0.3f);
            else y = 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;
            #endregion

            #region 不使用平滑插值
            //targetY = ClampAngle(targetY, yMinLimit, yMaxLimit);
            //Quaternion rotation1 = Quaternion.Euler(targetY, targetX, 0);
            //distance = Mathf.SmoothDamp(distance, targetDistance, ref zoomVelocity, 0f);
            //Vector3 position1 = rotation1 * new Vector3(0.0f, 0.0f, -distance) + pivot.position + pivotOffset;
            //transform.rotation = rotation1;
            //transform.position = position1; 
            #endregion
        }
    }


    /// <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);
    }
}


拥有自己的服务器

让开发工作不再难

MyBe

阿里云 —ESC服务器部署和搭建购买方式(图文并排,一目了然)

一键领取阿里全产品2000元优惠券大礼包 (新手必得享超值优惠)


本博客为非营利性个人原创
所刊登的所有作品的著作权均为本人所拥有
本人保留所有法定权利,违者必究!
对于需要复制、转载、链接和传播博客文章或内容的
请及时和本博主进行联系,留言,Email: ChinazJacob@163.com
————————————————————————————————
版权声明:对于经本博主明确授权和许可使用文章及内容的
使用时请注明文章或内容出处并注明网址
转载请附上原文出处链接及本声明。
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Alxes_七局

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

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

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

打赏作者

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

抵扣说明:

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

余额充值