|
|
Chinar 教程效果:
1
ScriptMount —— 脚本挂载相机
很多新手对于相机视角的控制、都不是太了解
这里Chinar给新手提供一个简单易懂的,拿着就能用的例子!
创建脚本,复制以下代码,并挂在相机上!
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) —— 英文变量
英文变量,直接可入工程
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 中含有小球变色的脚本,这里都不提供了
中文变量,仅仅是为了方便新手理解。
如果在工程中,希望大家养成用英文声明变量的好习惯!
支持
May Be —— 搞开发,总有一天要做的事!
|
本博客为非营利性个人原创,除部分有明确署名的作品外,所刊登的所有作品的著作权均为本人所拥有,本人保留所有法定权利。违者必究 对于需要复制、转载、链接和传播博客文章或内容的,请及时和本博主进行联系,留言,Email: ichinar@icloud.com 对于经本博主明确授权和许可使用文章及内容的,使用时请注明文章或内容出处并注明网址