- Home /
How to create meteors everywhere without making the game slow?
I am new to the community. I had by accidently made a thread on this on http://forum.unity3d.com/threads/170642-Meteors So here's what I had written... I have a controllable jet which constantly moves, and I want to make meteors everywhere. I have a prefab of the meteor. Of course I can't make gazillion meteors everywhere in the scene, so I am planning to make new meteors appear in front of the jet and the meteors behind the jet to just get destroyed. How do I do this? The jet must be able to crash in on the meteors. I normally do code in C# but UnityScript (JavaScript) is fine...
Answer by robertbu · Feb 16, 2013 at 04:36 PM
There is a lot you can do. You are on the right track by have a limited number of meteors and moving and moving them in front of the jet. To determine if an object an be destroyed I would first check if it can be seen by the camera. There are a variety of ways: Take a look at the following. OnWillRenderObject, Renderer.isVisible, Renderer.OnBecameVisible, and OnBecameInvisible.
From this list of not visible meteors, I'd cull any objects that are unlikely to be seen and place them in front of the camera. Convert their positions to Viewport coordinates (Camera.WorldToViewportPoint()) Points that can be seen with have x and y values in the range of 0 to 1, and a positive Z value. Anything with a negative z value will be behind the camera. Anything with a value far outside the 0 to 1 range is highly unlikely to be seen by the ship even if it is turned somewhat.
You can use Viewport coordinates in placing the meteors to assure that they will be in or near the field of view.
As for making the meteors display efficiently, take a look at this page:
There are limits (such as mesh complexity) to what can be batched, but when it usable, speed improvement can be substantial.
Thanks robertbu!!! I thought about the OnBecameInvisible() {/ Destroy object Script/} But if I turn the ship 90 degrees in the game will there be any meteors seen? how do I make the x, y and z coordinates of the meteors random between the viewport in the axis (not including the other 2 axes) from the ship without making them collide? By the way I didn't understand the Draw Call Batching, I'm just 11 years old. Thanks a lot!!!...
Congrats on being 11 and tackling coding a project like this.
In order to make suggestions, I need to know a bit more. What is moving in the scene, the meteors? The ship? How are things moving (i.e. you are moving them through the transform, or are you using Rigidbodies and force)? What is your target platform (PC, Web, mobile). The ship can obviously turn. Can it turn a full 360? And is this first person or third person?
A few things to mention since your original question was about performance. I don't know what you used to model your meteor, but if your meteor (technically I think they are asteroids) can be kept simple enough, and if you use the same material for all your meteors, then the drawing of the asteroids will be batched together by Unity. The limit is 900 vertex attributes in total, which usually means 450 to 300 vertices. You can see major performance improvements by having Unity batch object together. Select your mesh in Unity, and it will tell you how many vertices and triangles are in the mesh.
As for your destroy code. I would create all my meteors at the beginning of the game, and never destroy them. I'd just move them. I'd check that both the distance is too long and that it is not visible. If the the meteor is visible, it is likely your ship is moving in that direction. At the top of my meteor script, I'd initialize a variable in Start() (assumes the name of the game object is "Ship"):
goShip = GameObject.Find("Ship");
Then inside update you can check:
if (!renderer.isVisible)
{
float dist = Vector3.Distance(gameObject.transform.position, goShip.transform.position);
if (dist > /* some number */)
{
// $$anonymous$$ove the ship.
}
}
Thanks a lot robertbu !!! Now I can make my game properly.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092812im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to tell at runtime if a GameObject is a prefab 3 Answers
Link GUIText to a prefab? 2 Answers