- Home /
2d multiple falling objects
Hey guys,
I am trying to make a little game where objects fall from the top of the camera. When they are falling you can click on them to destroy them.
This is working well when i am using 10 objects but when i make 100 objects or let them fall in repeat mode the onMouseDown() event is not triggered.
I'm new to Unity and i was wondering if there is something i must be looking for to investigate what is the issue here.
I have the following :
A gameObject that has a script to make the game objects. I tried to make them in waves with WaitForSeconds, a loop that makes all the objects but then above each other or a invokeRepeating method with a delay.
My figures are made with a Prefab that has a script that uses the OnMouseDown() to destroy the gameObject. I also tried the normal gravity of a object and
transform.Translate(Vector3.down * fallSpeed * Time.deltaTime, Space.World);
for consistent falling speed. I also tried the Raycast with :if (Input.GetMouseButtonDown(0)) { Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(worldPoint, new Vector3(0,0,1)); Debug.Log("Click"); if (hit.collider != null && hit.collider.gameObject.tag == "Figure") { Debug.Log("Destroy : " + hit.collider.name); Destroy(hit.collider.gameObject); } }
So a small repeat. My problem is that with allot of objects that are falling the click of the mouse does not correctly trigger a destroy on different ways. Appreciate the help thanks :D
You could try a raycasting to a specific physics layer ins$$anonymous$$d of using a tag, this might help
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero, $$anonymous$$athf.Infinity, Layer$$anonymous$$ask.NameToLayer("Figure"));
if (hit)
{
Destroy(hit.collider.gameObject);
}
}
you might also consider pooling your objects ins$$anonymous$$d of creating and destroying them https://unity3d.com/learn/tutorials/topics/scripting/object-pooling
Answer by Smobel · Sep 26, 2017 at 04:20 PM
I followed one of the unity tutorials and there they made a Boundery over the whole background with a OnTriggerExit2d event. This made the game performance bad. I changed this to a smaller box collider under the camera with a onTriggerEnter2d to remove the objects.
I found the problem with the script from @Nkoeppel because my layers where not set up right yet and deleted my boundery. Then everything worked but my boundery collision. Then i fixed the boundery collision and the game worked allot better.
Thanks for the Help guys.
Answer by Timo326 · Sep 26, 2017 at 08:28 AM
How many frames per second are rendered when you have many objects? If you have low fps it could happen that you click between two frames...
Answer by goutham12 · Sep 26, 2017 at 10:06 AM
don't use that many objects. use polling. use 10 to 15 objects repet the same objects. like
list fallObjects = new ();
void Start(){
for(int i = 0; i < 10; i++){
GameObject g = instatiate("your prefab");
g.setative(false);
fallObjects.add(g);
}
}
void Update(){
if(input.GetMouseButtonDown(0)){
GameObject g = GetObjectFromPool ();
// position this guy in your needs
} }
GameObject GetObjectFromPool (){
for(int i = 0; i< fallObjects.count; i++){
if(! fallObjects [i].activeinhirachie){
fallObjects [i].setactive(true);
return fallObjects [i];
} } GameObject g = instatiate("your prefab");
g.setative(true);
fallObjects.add(g);
return g; }
// NOTE Don't Forget to deactivate objects when they are out of camera
Your answer
Follow this Question
Related Questions
Two different Raycast 2D are hitting at the same time when i touch one of them 1 Answer
Remove trees dynamically - stuck with collider 3 Answers
ON hit remove 1 Answer
How do I remove a GameObject var? 1 Answer
How do I remove this text box 1 Answer