小白的Unity5之路(二)镜头平滑跟随角色

这次要完成Camera跟随Player移动,

首先考虑Camera的跟随目标target和平滑移动速度smothing再考虑CameraPlayer的偏移量(就是CameraPlayer有一个永恒的长度)

 

目标位置和平滑移动速度 

public Transform target;

public float smothing = 5f;

 

偏移量

Vector3 offset;

 

设置偏移量

void Start () {

     offset = transform.position - target.position;

}

 

随着Player的移动,摄像机需要移动到新的位置

Vector3 targetCampos = target.position + offset;

得到Camera要移动的位置后

 transform.position=Vector3.Lerp(transform.position,targetCampos,smothing* Time.deltaTime);

 

则完整代码

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class CameraFloor : MonoBehaviour {
 5     public Transform target;
 6     public float smothing = 5f;
 7     Vector3 offset;
 8     // Use this for initialization
 9     void Start () {
10         offset = transform.position - target.position;
11     }
12     // Update is called once per frame
13     void FixedUpdate () {
14         Vector3 targetCampos = target.position + offset;
15         transform.position = Vector3.Lerp(transform.position, targetCampos, smothing * Time.deltaTime);//摄像机自身位置到目标位置
16     }
17 }

 

转载于:https://www.cnblogs.com/kerrysx/p/4750010.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值