- Home /
A lot of objects in 1 scene
I'm making a top down space game, and I've made a script that will generate stars in a specific area. Now if I pick a really big area, say from x-1000 to x1000 and y-1000 to y1000, and generate a lot of stars in there. The game starts lagging, because I have all the stars bound to an empty object, and the empty object has a script attached to it. Which will make the stars stay in the background and move slower according to the movement of the ship. Now my question is, is there any way to make it so that the game doesn't lag when there are a lot of moving objects involved?
Answer by LaneFox · Jul 01, 2015 at 11:12 AM
Creating individual gameobjects for something as prolific as stars is not a good idea.
Why don't you just make a few textures and layer them at different depths so they move at various rates relative to the ship? This is much more standard, reduces load and is easier to do.
How do I layer them so that they move at various rates? Because I did some testing, and I found out that when I take the script that makes the stars move slower off. The lag disappears. I'll link the script.
var ship : Rigidbody2D;
var scrollingSpeed : float = 300;
function Start () {
ship = GameObject.Find("test_ship").GetComponent.<Rigidbody2D>();
}
function Update () {
transform.Translate(Vector2.up * ship.velocity.y * Time.deltaTime / scrollingSpeed, Camera.main.transform);
transform.Translate(Vector2.down * -ship.velocity.y * Time.deltaTime / scrollingSpeed, Camera.main.transform);
transform.Translate(Vector2.right * ship.velocity.x * Time.deltaTime / scrollingSpeed, Camera.main.transform);
transform.Translate(Vector2.left * -ship.velocity.x * Time.deltaTime / scrollingSpeed, Camera.main.transform);
}
If you make the planes farther away (using a perspective camera) you will automatically get a parallax effect. This works with the new 2D system as well.
The reason that script is lagging your game is because there are so many stars.
It worked man great! After switching to perspective view and putting the plane where the stars are on further back, the lag stopped completely.
Your answer
Follow this Question
Related Questions
Attaching a script to a second object ruins everything 1 Answer
'text' is not a member of 'OBJECT' JavaScript Not Working in Unity 3.5 - Lost Commands? 2 Answers
Simple way to make an object go up and down?(Y axis) 1 Answer
Making a ball move towards the mouse? 1 Answer
Enable objects to enter the trigger 2 Answers