【问题标题】:Random Script repeatedly going down随机脚本反复下降
【发布时间】:2022-01-05 11:10:13
【问题描述】:

我正忙于构建平台游戏(2d)。我真的想要程序生成。我想我想出了一个随机生成平台的智能系统(有待讨论)。但我的平台不断选择随机生成。不管我重新开始游戏多少次 例如:

这是我的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlatformSpawner : MonoBehaviour
{
public GameObject[] platform;
public Vector2 SpawnGap;
public float randomspawn;
public float VerticalRandomness;
public float queueTime = 1.5f;
private float time = 0;
public Vector2 startspawn;
public GameObject flag;

Vector2 nextspawn;
Vector2 nextspawncentre;
Vector3 prevspawn;
public int platamount;
int amountspawned;
bool alreadyspawnedflag;
// Start is called before the first frame update
void Start()
{

    platamount = Random.Range(5, 15); // random amount of platforms in level
    alreadyspawnedflag = false; 
}

// Update is called once per frame
void Update()
{

    

    if (time > queueTime) // just for quality of life(so that if I maybe have 10000 platforms I can delay the spawning)
    {
        if(platamount >= amountspawned) // random amount of platform
        {
            GameObject go = Instantiate( platform[ Random.Range (0, platform.Length) ] ); // instantiating random object from list, to make it even more random(objects assigned in inspector)
            Vector2 randomrange = new Vector2(Random.Range(1f, -1f) * randomspawn, Random.Range(2f, -0.25f/*perferes up, but stil not working*/) * VerticalRandomness);//calculates random position

            Debug.Log(randomrange);// it is indeed choosing random pos, even on y but still giong down


            nextspawncentre = new Vector2(prevspawn.x + SpawnGap.x + startspawn.x, prevspawn.y + SpawnGap.y + startspawn.y); // calculates next spawn without randomness
            nextspawn = new Vector2(nextspawncentre.x + randomrange.x, nextspawncentre.y + randomrange.y);// adds randomness


            go.transform.position = transform.position + new Vector3(nextspawn.x, nextspawn.y, 0);// places object at nextspawn variable

            time = 0;// resets timer

            
            prevspawn = go.transform.position;
            
            amountspawned += 1;
            randomrange = new Vector2(0, 0);// tried reseting the random, but to no success
        }
        else if(!alreadyspawnedflag)// almost same logic as before just for the flag to reload scene(handeled in another script)
        {
            GameObject spawnedflag = Instantiate(flag);
            spawnedflag.transform.position = new Vector3(nextspawncentre.x + 1, 15, 0f);
            alreadyspawnedflag = true;
        }
        
    }

    time += Time.deltaTime;
}

}

看看 cmets 的解释,那里的一切。

提前感谢您的帮助。

ps:平台没有刚体,只有精灵渲染器和boxcollider2d。

【问题讨论】:

    标签: c# unity3d random procedural-generation


    【解决方案1】:

    我认为这个问题与您的 startSpawn 为负值有关(至少从您的第一个平台看起来是这样)。因此,即使 randomrange 中的 Y 为正值,它也可能被负的 startSpawn Y 值推翻。

    实际上,我认为您甚至根本不会每次都添加您的startSpawn,而只是将其用作初始位置

    我认为你应该简单地做例如

    private bool firstPlatform = true;
    

    ...
    if(firstPlatform)
    {
         nextspawn = startspawn;
         firstPlatform = false;
    }
    else
    {
        nextspawncentre = prevspawn + SpawnGap; 
        nextspawn = nextspawncentre + randomrange;
    }
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-13
      • 2021-02-20
      • 1970-01-01
      • 2018-10-25
      • 2011-07-04
      • 2016-05-16
      • 2019-06-19
      相关资源
      最近更新 更多