Unity相机平滑跟随
简介
unity中经常会用到固定视角的相机跟随,然后百度发现大家都是自己写的,然后偶也写咯一个,分享一下
PS: 由于刚学C#不久,才发现delegate这个东东,也不知道对性能影响大不大,但是看MS自己的界面库中各种使用,脑补了下估计可以用吧,就用了
Code
先上代码:
先是使用if进行判断的版本,支持实时锁定xyz的位置
using UnityEngine;
public class FixedFollowCamera : MonoBehaviour
{
// 需要跟随的目标对象
public Transform target;
// 需要锁定的坐标(可以实时生效)
public bool freazeX, freazeY, freazeZ;
// 跟随的平滑时间(类似于滞后时间)
public float smoothTime = 0.3F;
private float xVelocity, yVelocity, zVelocity = 0.0F;
// 跟随的偏移量
private Vector3 offset;
// 全局缓存的位置变量
private Vector3 oldPosition;
// 记录初始位置
private Vector3 startPosition;
void Start()
{
startPosition = transform.position;
offset = transform.position - target.position;
}
void LateUpdate()
{
oldPosition = transform.position;
if (!freazeX)
{
oldPosition.x = Mathf.SmoothDamp(transform.position.x, target.position.x + offset.x, ref xVelocity, smoothTime);
}
if (!freazeY)
{
oldPosition.y = Mathf.SmoothDamp(transform.position.y, target.position.y + offset.y, ref yVelocity, smoothTime);
}
if (!freazeZ)
{
oldPosition.z = Mathf.SmoothDamp(transform.position.z, target.position.z + offset.z, ref zVelocity, smoothTime);
}
transform.position = oldPosition;
}
/// <summary>
/// 用于重新开始游戏时直接重置相机位置
/// </summary>
public void ResetPosition()
{
transform.position = startPosition;
}
}
然后再来个delegate的版本,不支持实时锁定xyz的位置,具体性能升降也未知...但是感觉用着很不错的感觉
using UnityEngine;
// 更新位置委托,用于减少判断次数,具体性能升降未知
delegate void UpdatePosition();
public class FixedFollowCamera : MonoBehaviour
{
// 需要跟随的目标对象
public Transform target;
// 需要锁定的坐标(无法实时生效)
public bool freazeX, freazeY, freazeZ;
// 跟随的平滑时间(类似于滞后时间)
public float smoothTime = 0.3F;
private float xVelocity, yVelocity, zVelocity = 0.0F;
// 跟随的偏移量
private Vector3 offset;
// 全局缓存的位置变量
private Vector3 oldPosition;
// 记录初始位置
private Vector3 startPosition;
private UpdatePosition JudgePosition;
void Start()
{
startPosition = transform.position;
offset = transform.position - target.position;
// 分配事件
if (!freazeX)
{
JudgePosition += MoveX;
}
if (!freazeY)
{
JudgePosition += MoveY;
}
if (!freazeZ)
{
JudgePosition += MoveZ;
}
}
void LateUpdate()
{
oldPosition = transform.position;
JudgePosition();
transform.position = oldPosition;
}
private void MoveX()
{
oldPosition.x = Mathf.SmoothDamp(transform.position.x, target.position.x + offset.x, ref xVelocity, smoothTime);
}
private void MoveY()
{
oldPosition.y = Mathf.SmoothDamp(transform.position.y, target.position.y + offset.y, ref yVelocity, smoothTime);
}
private void MoveZ()
{
oldPosition.z = Mathf.SmoothDamp(transform.position.z, target.position.z + offset.z, ref zVelocity, smoothTime);
}
/// <summary>
/// 用于重新开始游戏时直接重置相机位置
/// </summary>
public void ResetPosition()
{
transform.position = startPosition;
}
}
使用方法
1.首先把摄像机和需要跟随的角色位置在unity编辑界面中调好
2.然后把代码拖到摄像机上,记得把需要跟随的角色也拖过去
然后就没有然后了,不出意外地话应该就能达到以下效果了
如有问题,肯定是你打开方式不对,也可以留言咨询 ╮(╯▽╰)╭
Unity相机平滑跟随的更多相关文章
- Unity3d学习 相机的跟随
最近在写关于相机跟随的逻辑,其实最早接触相机跟随是在Unity官网的一个叫Roll-a-ball tutorial上,其中简单的涉及了关于相机如何跟随物体的移动而移动,如下代码: using Unit ...
- Unity 3D里相机的平滑跟随(转)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 public class ...
- unity相机跟随Player常用方式
固定跟随,无效果(意义不大) public class FollowPlayer : MonoBehaviour { public Transform Player; private Vector3 ...
- Unity相机跟随
固定相机跟随 这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collections; public c ...
- Unity相机跟随-----根据速度设置偏移量
这里假设在水中的船,船有惯性,在不添加前进动力的情况下会继续移动,但是船身是可以360度自由旋转,当船的运动速度在船的前方的时候,相机会根据向前的速度的大小,设置相机的偏移量,从而提高游戏的动态带感. ...
- 小白的Unity5之路(二)镜头平滑跟随角色
这次要完成Camera跟随Player移动, 首先考虑Camera的跟随目标target和平滑移动速度smothing再考虑Camera与Player的偏移量(就是Camera与Player有一个永恒 ...
- unity, 相机空间 与 相机gameObject的局部空间
在unity里 相机空间 与 相机gameObject的局部空间 不重合. Camera.worldToCameraMatrix的文档中有这样一句话: Note that camera space m ...
- Unity 相机
相机属性 1.相机的Clear属性:Skybo背景会渲染天空盒:solid color背景为颜色:depth only仅仅深度,相当于优先级:Don`t Clear背景是上一帧的图像:2.Projec ...
- Unity 相机的聚焦功能
摘要:本文原创,转载请注明出处 需求: 在游戏的任务编辑场景进行编辑的时候,摄像机需要在多个需要编辑的物体之间来回切换,如果只是用摄像机的移动旋转,对于相对位置较近的物体还好说,当相对位置过远的时候, ...
随机推荐
- PAT 1139 First Contact
Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...
- 转载 - Struts2 拦截器详细配置过程
出处:http://www.blogjava.net/zzzlyr/archive/2009/10/12/297998.html Struts2 拦截器详细配置过程 1:所有拦截器的超级接口Inter ...
- 校长的收藏(洛谷 U4534)
题目背景 XS中学的校长喜欢收集手办,家里面都是价值不菲的手办. 校长喜欢给手办们排队并且对于某些些区间内的手办喜爱有加. 现在,校长外出散步(找乐子),你潜入他的房间打算借(偷走)他的手办炫耀一下. ...
- 关于static静态块的使用和static list的使用
public class CorsConfiguration { /** * Wildcard representing <em>all</em> origins, metho ...
- BP神经网络及其在教学质量评价中 的应用
本文学习笔记是自己的理解,如有错误的地方,请大家指正批评.共同进步.谢谢! 之前的教学质量评价,仅仅是通过对教学指标的简单处理.如求平均值或人为的给出各指标的权值来加权求和,其评价结果带有非常大主观性 ...
- 1. FrogRiverOne 一苇渡江 Find the earliest time when a frog can jump to the other side of a river.
package com.code; public class Test04_3 { public static int solution(int X, int[] A) { int size = A. ...
- iOS: 学习笔记, Swift操作符定义
Swift操作符能够自行定义, 仅仅须要加上简单的标志符就可以. @infix 中置运算. 如+,-,*,/运算 @prefix 前置运算. 如- @postfix 后置运算. a++, a-- @a ...
- Tomcat PK Resin
特征 Tomcat Resin 所属公司 Apache CAUCHO 用户数 多 少 可參考文档 多 少 与Eclipse集成复杂度 适中 较复杂. Eclipse下调试开发 简便 复杂.更新类后会自 ...
- <感悟帖>互联网与电子传统行业之经历
依据鄙人浅显的实习.毕业工作经验以及个人思考得此文. 鄙人有幸在BAT中的B实习,以及某电子行业科研机构工作,略有心得.总结例如以下.也算是对自己的一个交待和反省. 鄙人小硕毕业211学校非985,C ...
- LeetCode 500. Keyboard Row (键盘行)
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...