这里给主相机绑定一个脚本。

脚本写为:

using UnityEngine;
using System.Collections; public class camerafollow : MonoBehaviour {
//主摄像机跟随主角一起移动
public float xMargin = 1f;
public float yMargin = 1f;
public float xSmooth = 8f;
public float ySmooth = 8f;
public Vector2 maxXandY;
public Vector2 minXandY;
// Use this for initialization
private Transform player;
void Start () {
//这里获得是绑定的主角,需要一起跟随移动,就要获得主角的属性
player = GameObject.FindGameObjectWithTag("pk_0").transform;
maxXandY.x = 10; maxXandY.y = 10;
}
bool checkxmargin() {
return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
}
bool checkymargin() {
return Mathf.Abs(transform.position.y - player.position.y) > yMargin;
}
// Update is called once per frame
void Update () {
Trackplayer();
}
//跟踪主角
void Trackplayer() {
float targetx = transform.position.x;
float targety = transform.position.y;
if (checkxmargin())
{
targetx = Mathf.Lerp(transform.position.x, player.position.x, xSmooth * Time.deltaTime); }
if (checkymargin())
{
targety = Mathf.Lerp(transform.position.y, player.position.y, xSmooth * Time.deltaTime);
}
targetx = Mathf.Clamp(targetx, minXandY.x, maxXandY.y);
targety = Mathf.Clamp(targety, minXandY.y, maxXandY.y);
transform.position = new Vector3(targetx, targety,transform.position.z);
}
}

  效果图:

最新文章

  1. MFC去掉标题栏
  2. form-line 样式 让 两个控件在同一个水平位置
  3. GET和POST请求
  4. JavaWeb学习总结(一)——JavaWeb开发入门
  5. easyui datagrid 获取 title
  6. 【leetcode❤python】232. Implement Queue using Stacks
  7. HTML5塔防游戏——《三国塔防》 - Yorhom's Game Box
  8. CHM类型API文件打不开问题解决方法
  9. [C#]线程处理
  10. 部署MongoDB扩展并测试使用php简单连接操作之
  11. poj 3295 Tautology(栈)
  12. atitit.提升稳定性---hibernate 添加重试retry 机制解决数据库连接关闭
  13. jmeter 分布式压测(Linux)
  14. tensorflow 模型保存和加载
  15. Yii Cache 缓存的使用
  16. [转载]Oracle PL/SQL之LOOP循环控制语句
  17. slideDown留言板
  18. Go Example--Hello
  19. HappytimeOnvif Client V8.3的使用
  20. LOJ 3055 「HNOI2019」JOJO—— kmp自动机+主席树

热门文章

  1. tyvj P1209 - 拦截导弹 平面图最小割&&模型转化
  2. Codeforces Round #205 (Div. 2) : D
  3. html5自定义标签选择器
  4. 应用安全技术趋势之 Top 5
  5. 使用2to3将代码移植到Python 3-转
  6. [topcoder]CoinReversing
  7. I2C总线之(二)---时序
  8. SQL*Net more data to client
  9. gitweb安装
  10. dpkg error