Unity3D 相机跟随主角移动
2015-11-03 22:05
656 查看
这里给主相机绑定一个脚本。
脚本写为:
效果图:
脚本写为:
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); } }
效果图:
![](http://images2015.cnblogs.com/blog/637344/201511/637344-20151103220447274-1716957623.jpg)
相关文章推荐
- Unity3D Editor模式下批量修改prefab
- Unity3D 第三人称的游戏角色的单选和移动
- unity3d 文本多语言实现
- Unity3D 鼠标检测世界中的游戏对象
- Unity各种小技巧函数方法—RPG黑暗之光解析
- Cg shader with image-based diffuse lighting
- Unity3d 制作动态Mesh且可以随地面凹凸起伏
- unity3d 切换场景过度动画
- 怎样实现Update中方法只被调用一次
- unity连接sqlite数据库操作
- Cg shader for Unity-specific skybox
- Cg shader for skybox
- unity 2D 正交摄像机下 uGUi 比例与自适应问题
- UNITY3D 自定义Mesh
- Unity中HideInInspector和SerializeField
- 游戏摇杆之Easy Touch 3教程
- Unity中简单 UI 管理类 - UIManager
- Unity3D脚本技术框架
- Unity3d 引擎原理详细介绍
- Unity3D-深入剖析NGUI的游戏UI架构