统一产生和破坏随机平台

时间:2019-01-21 23:19:19

标签: c# visual-studio unity3d

我正试图统一制作一个涂鸦跳跃游戏,但我有一个问题。

如何更改脚本以在顶部生成其他平台并销毁主摄像机下的任何平台。我将放下我的代码以生成随机平台

public class GradinariuZAHAR : MonoBehaviour
{

    public Transform Player;
    public GameObject platformPrefab;
    private float minimY = 0.2f;
    private float maximY = 1.6f;
    public int numberOfPlatforms = 200;
    public float levelWidth = 3f;


    // Use this for initialization
    void Start()
    {
        GenerateChunk(.2f, 1.5f);
    }
    void GenerateChunk(float minY, float maxY)
    {

        Vector3 spawnPosition = new Vector3(0f, 0f, 0f);

        for (int i = 0; i < numberOfPlatforms; i++)
        {
            spawnPosition.y += Random.Range(minY, maxY);
            spawnPosition.x = Random.Range(-levelWidth, levelWidth);
            Instantiate(platformPrefab, spawnPosition, Quaternion.identity);
        }
    }

    void Update()
    {
        if (Player.position.y > 160) {
                Debug.Log("acum");
                minimY = maximY;
                maximY = maximY * 2;
                GenerateChunk(minimY, maximY);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

enter image description here

只需将以下脚本附加到您的平台即可。一旦整个精灵完全位于相机下方,它将完全摧毁平台,完全符合您的需求。

using UnityEngine;

public class Platform : MonoBehaviour
{

SpriteRenderer sprite;
float bottomOfScreen;

private void Start()
{
    sprite = GetComponent<SpriteRenderer>();

    var cam = Camera.main;
    var screen = new Vector2(Screen.width, Screen.height);
    var camWorldPos = cam.ScreenToWorldPoint(screen);
    bottomOfScreen = camWorldPos.y - cam.orthographicSize * 2;
}

private void Update()
{
    var height = sprite.bounds.size.y;
    var topOfPlatform = transform.position.y + height / 2;

    if (topOfPlatform < bottomOfScreen)
        Destroy(gameObject);
}
}