博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity 3D第三人称视角、用途广泛限定角度(视角不能360度翻转)
阅读量:7235 次
发布时间:2019-06-29

本文共 8414 字,大约阅读时间需要 28 分钟。

Unity第三人称相机视角控制


本文提供全流程,中文翻译。
Chinar 坚持将简单的生活方式,带给世人!
(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例)


Chinar —— 心分享、心创新!
助力快速完成第三人称视角的限定与实现
为新手节省宝贵的时间,避免采坑!

Chinar 教程效果:

这里写图片描述



全文高清图片,点击即可放大观看 (很多人竟然不知道)


1

ScriptMount —— 脚本挂载相机

很多新手对于相机视角的控制、都不是太了解

这里Chinar给新手提供一个简单易懂的,拿着就能用的例子!

创建脚本,复制以下代码,并挂在相机上!

举个栗子黑白88

using UnityEngine;/// /// 我用中文写的变量,为了方便新手理解,请自行改为自己所需变量/// 挂载到主相机/// public class ChinarCamera : MonoBehaviour{    public  Transform 被跟踪对象;                                                 // 设置被跟踪物体    public  Transform target;                                                     // 像一个被选中的对象(用于检查cam和target之间的对象)    public  float     moveSpeed                 = 10;                             // 设置相机移动速度        public  float     默认距离                      = 10.0f;                      // 距目标距离(使用变焦)    public  float     最小距离                      = 2f;                         //最小距离    public  float     最大距离                      = 15f;                        //最大距离    public  float     速度倍率                      = 1f;                         //速度倍率    public  float     X轴速度                      = 250.0f;                      //x速度    public  float     Y轴速度                      = 120.0f;                      //y速度    public  float     Y轴最大角度                    = -90f;                      //相机向下最大角度    public  float     Y轴向下最小角度                  = 90f;                     //相机向上最大角度    private Vector3   与0点偏移量                    = Vector3.zero;              // 与目标的偏移量    private float     x, y, 目标X, 目标Y, 目标距离, X速度 = 1f, Y速度 = 1f, 相对速度倍率 = 1f; //x变量、y变量、目标x、目标y、目标距离、x速度、y速度、速度倍率    private bool      是第一次开始;                                               //默认:不是    public  bool      允许Y轴旋转 = true;                                         //允许Y轴倾斜    void Start()    {        var angles = transform.eulerAngles;                                    //当前的欧拉角        目标X        = x = angles.x;                                           //给x,与目标x赋值        目标Y        = y = ClampAngle(angles.y, Y轴最大角度, Y轴向下最小角度); //限定相机的向上,与下之间的值,返回给:y与目标y        目标距离       = 默认距离;                                             //初始距离数据为10;        是第一次开始     = false;    }    ///     /// 写在Late中更合理    ///     void LateUpdate()    {        if (被跟踪对象) //如果存在设定的目标        {            float scroll            = Input.GetAxis("Mouse ScrollWheel"); //获取滚轮轴            if (scroll > 0.0f) 目标距离 -= 速度倍率;                              //如果大于0,说明滚动了:那么与目标距离,就减少固定距离1。就是向前滚动,就减少值,致使越来越近            else if (scroll < 0.0f)                目标距离 += 速度倍率;                          //距离变远                                              //否则            目标距离     =  Mathf.Clamp(目标距离, 最小距离, 最大距离); //目标的距离限定在2-15之间            if (Input.GetMouseButton(1)) //鼠标右键            {                目标X += Input.GetAxis("Mouse X") * X轴速度 * 0.02f; //目标的x随着鼠标x移动*5                if (允许Y轴旋转)                                     //y轴允许倾斜                {                    目标Y -= Input.GetAxis("Mouse Y") * Y轴速度 * 0.02f; //目标的y随着鼠标y移动*2.4                    目标Y =  ClampAngle(目标Y, Y轴最大角度, Y轴向下最小角度);       //限制y的移动范围在-90到90之间                }                x             = Mathf.SmoothDampAngle(x, 目标X, ref X速度, 0.3f);                if (允许Y轴旋转) y = Mathf.SmoothDampAngle(y, 目标Y, ref Y速度, 0.3f);                else y        = 目标Y;            }            Quaternion rotation = Quaternion.Euler(y, x, 0);            默认距离                = Mathf.SmoothDamp(默认距离, 目标距离, ref 相对速度倍率, 0.5f);            Vector3 position    = rotation * new Vector3(0.0f, 0.0f, -默认距离) + 被跟踪对象.position + 与0点偏移量;            transform.rotation  = rotation;            transform.position  = position;        }        // 当按住鼠标右键的时候            if (Input.GetMouseButton(0))        {            float h = Input.GetAxis("Mouse X") * moveSpeed * Time.deltaTime;            float v = Input.GetAxis("Mouse Y") * moveSpeed * Time.deltaTime;            transform.Translate(h, 0, v, Space.World);        }    }    ///     /// 限定一个值,在最小和最大数之间,并返回    ///     /// 角度    /// 最小    /// 最大    /// 
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); }}

2

Script Mount (English) —— 英文变量

英文变量,直接可入工程

举个栗子黑白88

using UnityEngine;/// /// Chinar相机脚本/// public class ChinarCamera : 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"); //获取滚轮轴            if (scroll > 0.0f) targetDistance -= zoomSpeed;                         //如果大于0,说明滚动了:那么与目标距离,就减少固定距离1。就是向前滚动,就减少值,致使越来越近            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之间                }            }            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;        }    }    ///     /// 限定一个值,在最小和最大数之间,并返回    ///     ///     ///     ///     /// 
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); }}

3

Duwamish —— 提供的例子

注意:

Demo 中含有小球变色的脚本,这里都不提供了

中文变量,仅仅是为了方便新手理解。

如果在工程中,希望大家养成用英文声明变量的好习惯!

举个栗子黑白88
这里写图片描述


支持

May Be —— 搞开发,总有一天要做的事!

拥有自己的服务器,无需再找攻略!
Chinar 提供一站式教程,闭眼式创建!
为新手节省宝贵时间,避免采坑!

1 ——
2 ——
3——
4 ——


70
Chinar


END

本博客为非营利性个人原创,除部分有明确署名的作品外,所刊登的所有作品的著作权均为本人所拥有,本人保留所有法定权利。违者必究

对于需要复制、转载、链接和传播博客文章或内容的,请及时和本博主进行联系,留言,Email: ichinar@icloud.com
对于经本博主明确授权和许可使用文章及内容的,使用时请注明文章或内容出处并注明网址

转载于:https://www.cnblogs.com/chinarbolg/p/9601430.html

你可能感兴趣的文章
移动互联网时代,如何颠覆式协同工作
查看>>
背水一战 Windows 10 (82) - 用户和账号: 获取用户的信息, 获取用户的同意
查看>>
discuz X3全局变量$_G
查看>>
Linux中更改转移mysql数据库目录的步骤
查看>>
AngularJs-04-模拟登陆
查看>>
Ubuntu安装ping工具
查看>>
Keepalived单实例简单环境搭建
查看>>
Publication的 immediate_sync 属性
查看>>
屌丝Cent OS服务解密
查看>>
linux下查看和添加PATH环境变量
查看>>
docker swarm集群部署
查看>>
linux命令:chroot ldd init系统启动流程,修改启动背景图
查看>>
BGP community
查看>>
centos ipmitool安装
查看>>
CPG 2018核心打造信息化平台 携蓝月亮、珀莱雅深入IT业务转型之路
查看>>
探秘Java9
查看>>
RMAN内部原理介绍
查看>>
如何绕过ORA-00701错误和降低bootstrap对象的高水位
查看>>
LVS集群实战
查看>>
linux虚拟化管理
查看>>