在游戏中通常会实现的效果是玩家主角移动的时候,背景也可以跟着移动,要实现这种效果其实就是获取主角的位置,然后再改变摄像机的位置就可以了,这就需要通过脚本来实现。这个脚本添加到摄像机的GameObject上,相当于摄像机的控制器。

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraController : MonoBehaviour
  5. {
  6. public PlayerStateController.playerStates currentPlayerState = PlayerStateController.playerStates.idle;
  7. public GameObject playerObject = null;//玩家游戏对象
  8. public float cameraTrackingSpeed = 0.2f;
  9. private Vector3 lastTargetPosition = Vector3.zero;//玩家最后的位置
  10. private Vector3 currTargetPosition = Vector3.zero;//玩家当前的位置
  11. private float currLerpDistance = 0.0f;
  12.  
  13. void Start()
  14. {
  15. Vector3 playerPos = playerObject.transform.position;//玩家的位置
  16. Vector3 cameraPos = transform.position;//相机的位置
  17. Vector3 startTargPos = playerPos;//玩家初始化位置
  18.  
  19. startTargPos.z = cameraPos.z;
  20. lastTargetPosition = startTargPos;
  21. currTargetPosition = startTargPos;
  22. currLerpDistance = 1.0f;
  23. }
  24.  
  25. void OnEnable()
  26. {
  27. PlayerStateController.onStateChange += onPlayerStateChange;
  28. }
  29.  
  30. void OnDisable()
  31. {
  32. PlayerStateController.onStateChange -= onPlayerStateChange;
  33. }
  34.  
  35. void onPlayerStateChange(PlayerStateController.playerStates newState)
  36. {
  37. currentPlayerState = newState;
  38. }
  39.  
  40. void LateUpdate()
  41. {
  42. onStateCycle();
  43.  
  44. currLerpDistance += cameraTrackingSpeed;
  45. // 取两个向量之间的值
  46. transform.position = Vector3.Lerp(lastTargetPosition, currTargetPosition, currLerpDistance);
  47. }
  48.  
  49. void onStateCycle()
  50. {
  51. switch (currentPlayerState)
  52. {
  53. case PlayerStateController.playerStates.idle:
  54. trackPlayer();
  55. break;
  56.  
  57. case PlayerStateController.playerStates.left:
  58. trackPlayer();
  59. break;
  60.  
  61. case PlayerStateController.playerStates.right:
  62. trackPlayer();
  63. break;
  64.  
  65. case PlayerStateController.playerStates.jump:
  66. trackPlayer();
  67. break;
  68.  
  69. case PlayerStateController.playerStates.firingWeapon:
  70. trackPlayer();
  71. break;
  72. }
  73. }
  74.  
  75. void trackPlayer()
  76. {
  77. Vector3 currCamPos = transform.position;//当前相机位置
  78. Vector3 currPlayerPos = playerObject.transform.position;//当前玩家位置
  79.  
  80. if (currCamPos.x == currPlayerPos.x && currCamPos.y == currPlayerPos.y)//位置一样,不移动
  81. {
  82. currLerpDistance = 1.0f;
  83. lastTargetPosition = currCamPos;
  84. currTargetPosition = currCamPos;
  85. return;
  86. }
  87.  
  88. currLerpDistance = 0.0f;
  89.  
  90. lastTargetPosition = currCamPos;//最后的位置为相机的位置
  91.  
  92. currTargetPosition = currPlayerPos;//当前的位置为玩家的位置
  93.  
  94. currTargetPosition.z = currCamPos.z;
  95. }
  96.  
  97. void stopTrackingPlayer()
  98. {
  99.  
  100. Vector3 currCamPos = transform.position;
  101. currTargetPosition = currCamPos;
  102. lastTargetPosition = currCamPos;
  103.  
  104. currLerpDistance = 1.0f;
  105. }
  106. }

如果要把背景的元素区分开来,不同的背景对象有不同的移动速度那么实现的方式会稍微复杂一点点。

1、首先得把背景的GameObject进行一下分类,如下所示:

2、给这个背景GameObject的分组添加一个脚本,也就是给_ParallaxLayers添加脚本,主要需要的参数就是摄像机对象、背景GameObject的分类数组、移动速度等。

脚本如下所示:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ParallaxController : MonoBehaviour
  5. {
  6. public GameObject[] clouds;//云层
  7. public GameObject[] nearHills;//近山
  8. public GameObject[] farHills;//远山
  9. public GameObject[] lava;//地面
  10.  
  11. // 移动的速度
  12. public float cloudLayerSpeedModifier;
  13. public float nearHillLayerSpeedModifier;
  14. public float farHillLayerSpeedModifier;
  15. public float lavalLayerSpeedModifier;
  16.  
  17. public Camera myCamera;
  18.  
  19. private Vector3 lastCamPos;
  20.  
  21. void Start()
  22. {
  23. lastCamPos = myCamera.transform.position;//获取相机的位置
  24. }
  25.  
  26. void Update()
  27. {
  28. Vector3 currCamPos = myCamera.transform.position;
  29. float xPosDiff = lastCamPos.x - currCamPos.x;//计算相机x轴的变化
  30.  
  31. adjustParallaxPositionsForArray(clouds, cloudLayerSpeedModifier, xPosDiff);
  32. adjustParallaxPositionsForArray(nearHills, nearHillLayerSpeedModifier, xPosDiff);
  33. adjustParallaxPositionsForArray(farHills, farHillLayerSpeedModifier, xPosDiff);
  34. adjustParallaxPositionsForArray(lava, lavalLayerSpeedModifier, xPosDiff);
  35.  
  36. lastCamPos = myCamera.transform.position;
  37. }
  38. // 数组来存储游戏对象
  39. void adjustParallaxPositionsForArray(GameObject[] layerArray, float layerSpeedModifier, float xPosDiff)
  40. {
  41. // 遍历改变精灵的位置
  42. for (int i = ; i < layerArray.Length; i++)
  43. {
  44. Vector3 objPos = layerArray[i].transform.position;
  45. objPos.x += xPosDiff * layerSpeedModifier;
  46. layerArray[i].transform.position = objPos;
  47. }
  48. }
  49. }

另外一种实现的方案脚本:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraFollow : MonoBehaviour
  5. {
  6. public float xMargin = 1f; // Distance in the x axis the player can move before the camera follows.
  7. public float yMargin = 1f; // Distance in the y axis the player can move before the camera follows.
  8. public float xSmooth = 8f; // How smoothly the camera catches up with it's target movement in the x axis.
  9. public float ySmooth = 8f; // How smoothly the camera catches up with it's target movement in the y axis.
  10. public Vector2 maxXAndY; // The maximum x and y coordinates the camera can have.
  11. public Vector2 minXAndY; // The minimum x and y coordinates the camera can have.
  12.  
  13. private Transform player; // Reference to the player's transform.
  14.  
  15. void Awake ()
  16. {
  17. // Setting up the reference.
  18. // 查找玩家游戏对象
  19. player = GameObject.FindGameObjectWithTag("Player").transform;
  20. }
  21.  
  22. // 检查边缘
  23. bool CheckXMargin()
  24. {
  25. // Returns true if the distance between the camera and the player in the x axis is greater than the x margin.
  26. // x轴变化的绝对值大于设定值
  27. return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
  28. }
  29.  
  30. // 检查边缘
  31. bool CheckYMargin()
  32. {
  33. // Returns true if the distance between the camera and the player in the y axis is greater than the y margin.
  34. // y轴变化的绝对值大于设定值
  35. return Mathf.Abs(transform.position.y - player.position.y) > yMargin;
  36. }
  37.  
  38. void FixedUpdate ()
  39. {
  40. TrackPlayer();
  41. }
  42.  
  43. void TrackPlayer ()
  44. {
  45. // By default the target x and y coordinates of the camera are it's current x and y coordinates.
  46. float targetX = transform.position.x;
  47. float targetY = transform.position.y;
  48.  
  49. // If the player has moved beyond the x margin...
  50. if(CheckXMargin())
  51. // ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
  52. // 在当前位置和最新位置之间插值
  53. // Time.deltaTime 增量时间 以秒计算,完成最后一帧的时间(只读)。使用这个函数使和你的游戏帧速率无关
  54. targetX = Mathf.Lerp(transform.position.x, player.position.x, xSmooth * Time.deltaTime);
  55.  
  56. // If the player has moved beyond the y margin...
  57. if(CheckYMargin())
  58. // ... the target y coordinate should be a Lerp between the camera's current y position and the player's current y position.
  59. targetY = Mathf.Lerp(transform.position.y, player.position.y, ySmooth * Time.deltaTime);
  60.  
  61. // The target x and y coordinates should not be larger than the maximum or smaller than the minimum.
  62. // 把目标值限制在固定的范围
  63. targetX = Mathf.Clamp(targetX, minXAndY.x, maxXAndY.x);
  64. targetY = Mathf.Clamp(targetY, minXAndY.y, maxXAndY.y);
  65.  
  66. // Set the camera's position to the target position with the same z component.
  67. // 设置相机的位置
  68. transform.position = new Vector3(targetX, targetY, transform.position.z);
  69. }
  70. }

[Unity2D]实现背景的移动的更多相关文章

  1. Unity2D 背景图铺满与Camera.Size的计算公式

    在unity制作2D游戏的教程,背景图sprite铺满显示时Camaer的Size调到多少合适,作个笔记. 资源参数 background.png 2048x640,Sprite的像素单位:100 调 ...

  2. unity2D背景移动补偿从而获得3d错觉效果

    2d平台跳跃游戏当相机移动的时候背景跟随进行微调移动,从而使得玩家获得3d的错觉 using System.Collections;using System.Collections.Generic;u ...

  3. unity2D限制位置的背景移动补偿效果

    有时候我们想要背景可以跟随相机移动补偿,但是又不想该背景物体离原来的位置太远,比如我们想要一棵树在一个房子的后面,然后使用相机补偿使其跟随移动,达到3D错觉效果,但是我们又不想该物体偏离房屋太远.假设 ...

  4. Unity2D多分辨率屏幕适配方案(转载)

    一下内容转自:http://imgtec.eetrend.com/forum/3992 此文将阐述一种简单有效的Unity2D多分辨率屏幕适配方案,该方案适用于基于原生开发的Unity2D游戏,即没有 ...

  5. [Unity2D]游戏引擎介绍

    由于手机游戏的流行,目前2D游戏开发的需求量也越来越大了,因此Unity3D游戏引擎也增加了2D游戏开发的支持,之前是可以通过第三方的2D游戏组件可以支持2D游戏开发,现在是官方的版本就支持了.Uni ...

  6. [原创]一种Unity2D多分辨率屏幕适配方案

    此文将阐述一种简单有效的Unity2D多分辨率屏幕适配方案,该方案适用于基于原生开发的Unity2D游戏,即没有使用第三方2D插件,如Uni2D,2D toolkit等开发的游戏,NGUI插件不受这个 ...

  7. 一种Unity2D多分辨率屏幕适配方案

    http://www.cnblogs.com/flyFreeZn/p/4073655.html 此文将阐述一种简单有效的Unity2D多分辨率屏幕适配方案,该方案适用于基于原生开发的Unity2D游戏 ...

  8. CSS3 background-image背景图片相关介绍

    这里将会介绍如何通过background-image设置背景图片,以及背景图片的平铺.拉伸.偏移.设置大小等操作. 1. 背景图片样式分类 CSS中设置元素背景图片及其背景图片样式的属性主要以下几个: ...

  9. 冒泡,setinterval,背景图的div绑定事件,匿名函数问题

    1.会冒泡到兄弟元素么? $(function(){ $("#a").click(function(){alert("a")}) $("#b" ...

随机推荐

  1. The CompilerVersion constant identifies the internal version number of the Delphi compiler.

    http://delphi.wikia.com/wiki/CompilerVersion_Constant The CompilerVersion constant identifies the in ...

  2. django-cms 代码研究(七)杂七杂八

    实体关系图 核心对象: cms_page/cms_placeholder/cms_cmsplugin. page模型类继承关系图 CMSPlugin&Placeholder模型类继承关系图 = ...

  3. javascript quine

    javascript有一些奇怪的性质,恩,比如说,非常容易写一个quine,即自己输出自己代码的东西. function a(){console.log(a.toString()+";a() ...

  4. 【系统】CentOS、Ubuntu、Debian三个linux比较异同

    CentOS.Ubuntu.Debian三个linux比较异同 2014-07-31 12:58             53428人阅读             评论(6)             ...

  5. Product of Array Exclude Itself

    Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WI ...

  6. Sybase IQ导出文件的几种方式

    IQ有四种方法,将表的数据导出为文本文件:1.重定向 SELECT * FROM TABLE1 ># D:MYDATATABLE1.TXT -- 文件生成在执行语句的客户端上 2.通过选项导出 ...

  7. wx.html2.WebView在 target="_blank" or rel="external" 没有反映的解决方法

    在wx.html2.EVT_WEBVIEW_LOADED中,用WebView.RunScript运行删除链接目标的脚本 javaScriptStr = '''function deleteBlank( ...

  8. HDU 5724 Chess (状态压缩sg函数博弈) 2016杭电多校联合第一场

    题目:传送门. 题意:有n行,每行最多20个棋子,对于一个棋子来说,如果他右面没有棋子,可以移动到他右面:如果有棋子,就跳过这些棋子移动到后面的空格,不能移动的人输. 题解:状态压缩博弈,对于一行2^ ...

  9. 2.简单工厂模式(Simple Factory)

    using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //如果 ...

  10. UVa 11524:In-Circle(解析几何)

    Problem EIn-CircleInput: Standard Input Output: Standard Output In-circle of a triangle is the circl ...