- Home /
how to limit the number of platforms,Adding a limit to the amount of a certain object on the level
so I've started coding recently and I got stuck on a platform that falls and is then instantiated to a new position. I managed to get it to spawn in a new position above the player and then falls when colliding with them however they also bounce on other platforms like the player does and also start to spawn loads after a while when i only want about 3 or 4 at most on the screen at one given time. i think it as something to do with making them spawn frozen and then becoming active again but not totally sure. sorry if this question is really broad and doesn't fit the title properly this is my first time posting a question.
using System.Collections; using System.Collections.Generic; using System.Linq.Expressions; using System.Security.Cryptography; using System.Threading; using UnityEngine; using Random = UnityEngine.Random;
public class destroy : MonoBehaviour { [SerializeField] FallingPlat falling42;
Rigidbody2D rb;
// private GameObject[] LimiterFalling; // public int CurrentFallingPlats = 1;
public GameObject sackboy;
public GameObject platformPreFab;
public GameObject springPreFab;
public GameObject FallingPlatPreFab;
public GameObject GoblinPreFab;
public GameObject PinkPreFab;
public GameObject SlimePreFab;
private GameObject myPlat;
// private bool SpawnPlatforms;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
falling42.isKinematic = true;
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name.StartsWith("platform"))
{
if (Random.Range(1, 1) == 1)
{
Destroy(collision.gameObject);
Instantiate(platformPreFab, new Vector2(Random.Range(-5.0f, 5.0f), sackboy.transform.position.y + (7 + Random.Range(0.5f, 0.0f))), Quaternion.identity);
}
else
{
collision.gameObject.transform.position = new Vector2(Random.Range(-5.0f, 5.0f), sackboy.transform.position.y + (7 + Random.Range(0.5f, 0.0f)));
}
}
else if (collision.gameObject.name.StartsWith("spring"))
{
if (Random.Range(1, 1) == 1)
{
collision.gameObject.transform.position = new Vector2(Random.Range(-5.0f, 5.0f), sackboy.transform.position.y + (7 + Random.Range(0.5f, 0.0f)));
}
else
{
Destroy(collision.gameObject);
Instantiate(springPreFab, new Vector2(Random.Range(-5.0f, 5.0f), sackboy.transform.position.y + (7 + Random.Range(0.5f, 0.0f))), Quaternion.identity);
}
}
if (collision.gameObject.name.StartsWith("Falling"))
{
if (Random.Range(1, 1) == 1)
{
Destroy(collision.gameObject);
Instantiate(FallingPlatPreFab, new Vector2(Random.Range(-5.0f, 5.0f), sackboy.transform.position.y + (7 + Random.Range(0.5f, 0.0f))), Quaternion.identity);
falling42.isKinematic = true;
}
else
{
collision.gameObject.transform.position = new Vector2(Random.Range(-5.0f, 5.0f), sackboy.transform.position.y + (7 + Random.Range(0.5f, 0.0f)));
}
,
Answer by unity_ek98vnTRplGj8Q · Sep 08, 2020 at 03:25 PM
Your code is a little confusing but I will try to answer your questions in a more general way, then we can talk about how to implement this into your current code in the comments. From my understanding though, it seems like you are trying to make an endless jumper game like doodle jump, and so my answer will assume this is the case. Is this correct?
How to limit the amount of objects in the level - the easiest thing to do here is just to make sure you correctly destroy game objects as they disappear below you. It seems to me like you are sometimes destroying objects, but sometimes moving them above you. My guess is that this isn't working quite like you expect it to. Let me offer a more simple solution. Simply add a script to all of your platform game objects that checks the y position relative to the player every frame, and destroys the platform once you are a certain height above them.
public destroyHeight = 10f; void Update(){ if((player.position.y - transform.position.y) > destroyHeight) Destroy(gameObject); }
That way the platforms take care of removing themselves, and now you only have to worry about spawning new platforms. You can do this however you want, because now the number of platforms is only dependent on how fast you spawn them as they will all remove themselves automatically once they go off the bottom of the screen. If this works for you the next step for optimization would be to use object pooling rather than creating and destroying new platform instances, but don't worry about that yet.
2 - If you don't want your platforms colliding with eachother as they are falling, you can put your platforms on their own Layer, then disable collisions between two objects in that layer by going to Edit->Project Settings-> Physics Settings and unchecking that collision in the collision matrix.
Hopefully this answers your question, if not let me know
sorry that i didnt say the type of game but you're correct it is like a doodle jump game. i tried the layering with collision matrix unchecked however it didnt do anything although i might have done it wrong ill keep trying with that. Also i already have a game object that follows the player and instantiates new platforms while getting rid of the old ones im not sure if this is the main source of the problem as its not normal platforms spawning that have problems. But more so that the falling type of platforms keep duplicating or spawning more then they should be as there is only one placed prefab that should keep getting replace and destroy while im testing it but it can spawn loads after a while and it breaks the game. overall what im more concerned with is that the platform keeps getting created more then it should be as the other platforms have no problems with the instantiate script that follows the player only the falling platforms do if you want to see the code for them just ask and ill put them in the comments. thanks for the reply hopefully i can sort it out with your help or others.
I was able to make it stop duplicating by changing the prefab to a variant of the other types of platforms and it work the only thing that doesn't work now is the collision of the falling platforms against the other types of platforms. so if you can somehow wrap your head around my messy code that be great.
Can you explain what you tried before with the collision matrix? If you put all the platforms into a layer (we can call it the "Platform" layer, then you go into the physics settings and uncheck the box that corresponds to collisions between "Platform" and "Platform" it will tell the physics system to ignore all collisions between any of your platforms. $$anonymous$$ake sure all of your platform prefabs are on the same layer (or, if they are on different layers, make sure that all of those different layers have collision disabled between them). Is this what you did?
so i made a layer and called it platformlayer and disabled the collision matrix part where the platformlayer intersected with each other and it didn't do anything that i could see as the falling platforms still interacted with other platforms and still triggered other falling platforms.
If you are constantly instantiating and destroying GameObjects then your game will freeze up sooner or later. It would be better to use an object pool which handles activating and deactivating the same GameObjects over and over.
thx for the info about it ill try that out after ive sorted out the falling platforms and fixed them.