Unity无尽的产卵者对齐问题

问题描述 投票:0回答:0

我正在尝试统一创建一个无尽的滚动条。

我面临的问题是在一些预制件之后,关卡会相互叠加。

每个关卡部分都是一个预制件,作为一个空的游戏对象有一个起点和一个终点。

using System.Collections.Generic;
using UnityEngine;

public class LevelManager : MonoBehaviour
{
    [System.Serializable]
    public class DifficultySettings
    {
        public List<GameObject> prefabs;
        public float speed;
    }

    public List<DifficultySettings> difficultySettings;
    public int currentDifficulty;
    private Vector3 lastEndPoint;

    private List<GameObject> activePrefabs;

    public float spawnThreshold = 50f; 
    public float maxSpawnX = 30f;
    
    private void Start()
    {
        currentDifficulty = 0;
        activePrefabs = new List<GameObject>();
        lastEndPoint = transform.position;

        int initialIndex = Random.Range(0, difficultySettings[currentDifficulty].prefabs.Count);
        GameObject initialPrefab = Instantiate(difficultySettings[currentDifficulty].prefabs[initialIndex]);
        initialPrefab.transform.position = lastEndPoint;
        activePrefabs.Add(initialPrefab);

        SpawnPrefab();
    }


private void Update()
{

    for (int i = activePrefabs.Count - 1; i >= 0; i--)
    {
        GameObject prefab = activePrefabs[i];
        prefab.transform.position += Vector3.left * difficultySettings[currentDifficulty].speed * Time.deltaTime;

        // Delete the prefab if it's too far to the left
        if (prefab.transform.position.x + prefab.GetComponent<LevelSection>().GetEndPoint().localPosition.x < Camera.main.transform.position.x - Camera.main.orthographicSize * Camera.main.aspect)
        {
            activePrefabs.RemoveAt(i);
            Destroy(prefab);
        } 
    }


    // Spawn a new prefab if necessary
    GameObject lastPrefab = activePrefabs[activePrefabs.Count - 1];
    float lastPrefabEndPointX = lastPrefab.GetComponent<LevelSection>().GetEndPoint().position.x;
    float distanceToCameraRightEdge = Camera.main.transform.position.x + Camera.main.orthographicSize * Camera.main.aspect;

    if (distanceToCameraRightEdge - lastPrefabEndPointX < spawnThreshold && lastPrefabEndPointX < 150)
    {
        SpawnPrefab();
    }
}

private void SpawnPrefab()
{
    int randomIndex = Random.Range(0, difficultySettings[currentDifficulty].prefabs.Count);
    GameObject newPrefab = Instantiate(difficultySettings[currentDifficulty].prefabs[randomIndex]);
    LevelSection section = newPrefab.GetComponent<LevelSection>();

    // Get the start point and end point of the new prefab
    Vector3 newPrefabStartPoint = section.GetStartPoint().position;
    Vector3 newPrefabEndPoint = section.GetEndPoint().position;

    // Get the start point and end point of the previous prefab
    GameObject lastPrefab = activePrefabs[activePrefabs.Count - 1];
    LevelSection lastSection = lastPrefab.GetComponent<LevelSection>();
    Vector3 lastPrefabStartPoint = lastSection.GetStartPoint().position;
    Vector3 lastPrefabEndPoint = lastSection.GetEndPoint().position;

    // Convert local positions to world positions
    newPrefabStartPoint = lastPrefab.transform.TransformPoint(newPrefabStartPoint);
    newPrefabEndPoint = lastPrefab.transform.TransformPoint(newPrefabEndPoint);
    lastPrefabStartPoint = lastPrefab.transform.TransformPoint(lastPrefabStartPoint);
    lastPrefabEndPoint = lastPrefab.transform.TransformPoint(lastPrefabEndPoint);

    // Calculate the position offset and set the new prefab's position
    float overlap = 0.1f; // Set the overlap between prefabs
    float positionOffset = (lastPrefabEndPoint.x - lastPrefabStartPoint.x) + (newPrefabEndPoint.x - newPrefabStartPoint.x) - overlap;
    newPrefab.transform.position = lastPrefab.transform.position + Vector3.right * positionOffset;

    //Debug.Log("lastPrefabEndPoint: " + lastPrefabEndPoint);
    //Debug.Log("positionOffset: " + positionOffset);
    //Debug.Log("newPrefab.transform.position: " + newPrefab.transform.position);
    newPrefab.name = $"Prefab_{randomIndex}_{Time.time}";

    activePrefabs.Add(newPrefab);

    // Update the lastEndPoint
    Vector3 endPointWorldPosition = section.GetEndPoint().position;
    lastEndPoint = endPointWorldPosition;
}

}



每个部分还附有此脚本以获取起点和终点

using UnityEngine;

public class LevelSection : MonoBehaviour
{
    public Transform GetStartPoint()
    {
        return transform.Find("StartPoint");
    }

    public Transform GetEndPoint()
    {
        return transform.Find("EndPoint");
    }
}

任何帮助都会得到极大的帮助,我对 unity 还是个新手,虽然这将是一个简单的脚本,但是我整天都在努力让它工作。

screenshots

我曾尝试使用每个关卡预制件的边界框,但没有成功。

c# unityscript
© www.soinside.com 2019 - 2024. All rights reserved.