我一直在尝试使用Unity 2d实现无限视差。我已经启用了视差功能,但是在尝试使背景无限连续时,我开始遇到问题。
我应该提到,相机背景仅在X轴上受到限制。播放器可以上下移动,但背景和相机Y位置保持不变。
主要问题是,在我的11个用于视差效果的背景中,只有第一个被克隆,并且在屏幕外时都没有一个被销毁(尽管这是第二点)。 / p>
我尝试了几种不同的方法来检测播放器是否在后台。我使用了附加到每个背景对象的GameObjects,并将它们与Player的位置进行了比较。效果不佳,因此在我当前的版本中,我正在使用摄像头来检测播放器的位置。
我创建无限背景的过程如下:
Loop: Through all the background images and do the following checks:
If: Player position is outside the bounds of the current background on the left.
Then: Create a copy of the current background and place it to the left the current background.
If: Player position is outside the bounds of the current background on the right.
Then: Create a copy of the current background and place it to the right the current background.
要清理背景,我只检查播放器是否位于背景之一之前,如果没有,则将其删除。
以下是工作视差代码以及底部尝试的无限背景代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParallaxBackground : MonoBehaviour {
public GameObject[] backgrounds;
public float smooting = 1f;
public int offset = -1;
public Transform cam;
public Camera Lcam;
public Camera Rcam;
//private Transform cam;
private Vector3 previousCamPos;
private float[] parallaxScales;
private GameObject[] backgroundClones;
// Use this for initialization
void Start () {
previousCamPos = cam.position;
parallaxScales = new float[backgrounds.Length];
for (int i = 0; i < backgrounds.Length; i++) {
parallaxScales[i] = backgrounds[i].transform.position.z * offset;
}
}
// Update is called once per frame
void FixedUpdate () {
// Handle paralaxing
for (int i = 0; i < backgrounds.Length; i++) {
float parallax = (previousCamPos.x - cam.position.x) * parallaxScales[i];
float backgroundTargetPosX = backgrounds[i].transform.position.x + parallax;
Vector3 backgroundTargetPos = new Vector3(backgroundTargetPosX, backgrounds[i].transform.position.y, backgrounds[i].transform.position.z);
backgrounds[i].transform.position = Vector3.Lerp(backgrounds[i].transform.position, backgroundTargetPos, smooting * Time.deltaTime);
}
/* Handle continuous backgrounds*/
for (int i = 0; i < backgrounds.Length; i++)
{
// Left Camera
float pointL = Lcam.WorldToViewportPoint(backgrounds[i].transform.position).x;
// Right Camera
float pointR = Rcam.WorldToViewportPoint(backgrounds[i].transform.position).x;
// Check if the object is within the Left camera
if (pointL < 1)
{
Destroy(backgroundClones[i]);
var clone = Instantiate(backgrounds[i].gameObject);
clone.transform.position = Rcam.transform.position;
backgroundClones[i] = clone;
}
else if (pointR > 0)
{
Destroy(backgroundClones[i]);
var clone = Instantiate(backgrounds[i].gameObject);
clone.transform.position = Lcam.transform.position;
backgroundClones[i] = clone;
}
}
/* Handle removing backgrounds*/
for (int i = 0; i < backgrounds.Length; i++)
{
// Main Camera
Vector3 point = Camera.main.WorldToScreenPoint(backgrounds[i].transform.position);
// Left Camera
float pointL = Lcam.WorldToViewportPoint(backgrounds[i].transform.position).x;
// Right Camera
float pointR = Rcam.WorldToViewportPoint(backgrounds[i].transform.position).x;
// Check if the object is within the main camera
if (point.x > 0 || point.x < 1)
{
}
else {
Destroy(backgrounds[i]);
}
}
previousCamPos = cam.position;
}
}