Unity中SmoothDamp 平滑阻尼--相机跟随角色移动的示例
2017-12-12 17:38
1451 查看
Unity中使用Vector3.SmoothDamp(平滑阻尼)方法进行跟随移动,可以使跟随看起来很平滑,而不显得突兀,最典型的示例就是相机平滑跟随角色移动。
static function SmoothDamp (current : Vector3, target : Vector3, ref
currentVelocity : Vector3, smoothTime :
float, maxSpeed : float = Mathf.Infinity, deltaTime :
float = Time.deltaTime)
: Vector3
各参数含义:
1.current
当前物体位置
2.target
目标物体位置
3.ref
currentVelocity
当前速度,这个值由你每次调用这个函数时被修改(注:ref 关键字指参数按引用传递, 按引用传递的效果是,对所调用方法中参数进行的任何更改都反映在调用方法中。)
4.smoothTime
到达目标的时间,较小的值将快速到达目标
5.maxSpeed
所允许的最大速度,默认无穷大
6.deltaTime
自上次调用这个函数的时间,默认为Time.deltaTime
相机跟随角色移动的示例代码:
public class FllowTarget : MonoBehaviour {
public Transform charactor;//目标点的参考点
private float smoothTime = 0.01f;//移动所需的时间,值越小越快移动到目标处
private Vector3 currentVelocity=Vector3.zero;//当前速度,这个值每次调用SmoothDamp这个函数时被修改
private Camera mainCamera;//挂载的相机
void Awake () {
mainCamera = Camera.main;//初始化
}
void Start(){
上述示例的目标点为charactor.position + new Vector3(0, 1, -10),即角色的后上方而不是角色位置处;charactor是public类型的,可以从Unity界面将角色物体直接拖拽指定,也可以在脚本中获取。
static function SmoothDamp (current : Vector3, target : Vector3, ref
currentVelocity : Vector3, smoothTime :
float, maxSpeed : float = Mathf.Infinity, deltaTime :
float = Time.deltaTime)
: Vector3
各参数含义:
1.current
当前物体位置
2.target
目标物体位置
3.ref
currentVelocity
当前速度,这个值由你每次调用这个函数时被修改(注:ref 关键字指参数按引用传递, 按引用传递的效果是,对所调用方法中参数进行的任何更改都反映在调用方法中。)
4.smoothTime
到达目标的时间,较小的值将快速到达目标
5.maxSpeed
所允许的最大速度,默认无穷大
6.deltaTime
自上次调用这个函数的时间,默认为Time.deltaTime
相机跟随角色移动的示例代码:
public class FllowTarget : MonoBehaviour {
public Transform charactor;//目标点的参考点
private float smoothTime = 0.01f;//移动所需的时间,值越小越快移动到目标处
private Vector3 currentVelocity=Vector3.zero;//当前速度,这个值每次调用SmoothDamp这个函数时被修改
private Camera mainCamera;//挂载的相机
void Awake () {
mainCamera = Camera.main;//初始化
}
void Start(){
charactor=GameObject.Find("Player").GetComponent<Transform>();//查找到对象名为“Player”的物体获取其上的Transform组件 void Update () { //SmoothDamp方法的调用:在smoothTime时间内从当前点以当前速度移动到目标点 transform.position=Vector3.SmoothDamp(transform.position, charactor.position + new Vector3(0, 1, -10),ref currentVelocity, smoothTime); } }
上述示例的目标点为charactor.position + new Vector3(0, 1, -10),即角色的后上方而不是角色位置处;charactor是public类型的,可以从Unity界面将角色物体直接拖拽指定,也可以在脚本中获取。
相关文章推荐
- unity 相机平滑跟随游戏角色
- 【Unity&摄像机&NGUI&2.5D】UILabel跟随角色移动和其参数调整
- unity 如何控制摄像机跟随角色移动
- 【Unity快速实现小功能】实现摄像机跟随角色移动功能
- unity笔记 0 - 添加物体,预制体;施加力;移动物体,旋转物体;相机跟随;禁用物体
- 【Unity快速实现小功能】动画系统学习小技巧篇(二)——摄像机跟随角色移动
- unity 赛车相机跟随物体移动
- Unity平滑相机跟随
- Unity摄像机跟随角色移动
- Unity 相机跟随人物移动而移动,利用插值实现
- Unity3D自学笔记——架构应用(八)人物移动与角色相机的跟随
- Unity中实现相机平滑且流畅的跟随玩家(或其他对象)
- unity3d相机跟随物体平滑移动(C#代码)
- unity 相机平滑的接近物体脚本 Mathf.Smooth
- 相机跟随物体平滑移动旋转
- Unity中摄像机平滑跟随主角移动
- unity 相机跟随物体(角色)
- unity之--相机跟随移动
- 初学VR(四):如何让相机跟随着角色移动
- 转载他人:角色移动和相机跟随