Search Unity

Help with infinite space background

Discussion in '2D' started by KEELAN, Aug 17, 2014.

  1. KEELAN

    KEELAN

    Joined:
    Oct 26, 2013
    Posts:
    51
    Hi all,

    I am trying to acheive a similar effect found here with the background stars.


    Im only talking about right in the beginning, ie without any parallax effects. just the random generation and infinite size.

    There is a screenshot of my current attempt,
    It works by generating 1000 randomly placed stars within a circle of a certain radius, there is a circle collider set as a trigger attached to the player and whenever a star leaves the trigger it gets deleted and and a new star instantiated on the other side of the trigger, which means all stars have to have rigidbodies (as far as i know? ) . This does work but it comes at a serious overhead (especially just being the background) and if the number of stars increases any other 1000 the framerate drops substantially.

    Can anyone help with ideas on how to achieve this more efficiently?
     

    Attached Files:

  2. Punchbag

    Punchbag

    Joined:
    Oct 30, 2012
    Posts:
    48
    You'd be better off using Graphics.DrawMesh to render the stars to screen in code, rather than moving 1000s star objects around. This way you can write a function that draws the visible stars from point a (the camera focus) up to range r (the maximum visible range (the corner of the screen to the centre)).

    I know very little about Graphics.DrawMesh, but this should get you started:

    http://docs.unity3d.com/ScriptReference/Graphics.DrawMesh.html

    ating a
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    How about just placing particle system there?

    Does the stars (background) need to move or just the player & camera?
     
  4. KEELAN

    KEELAN

    Joined:
    Oct 26, 2013
    Posts:
    51
    @Punchbag
    Thanks, I will definitely check that out for the rendering. I am still not too sure how i would generate the stars though.

    @mgear
    I havent really worked much with the particle system before,
    The stars will be static (i am thinking about eventually adding in a parallax effect though)
    But if i do do the infinite space, then i will have to keep the player and camera stationary and move the world. so then the stars will move.
     
  5. Punchbag

    Punchbag

    Joined:
    Oct 30, 2012
    Posts:
    48
    Keep it simple at first, just write an algorithm that says:
    • For given camera position x/y, draw a star at every whole 80 pixels from origin that is on-screen. (that's whole 80 pixels from world origin, not the camera's focus, or they'll move with the camera!)
    This will give you an endless grid of stars. You can then tweak the formula to have them more sporadically displayed, but still consistently generated.
     
  6. Nothing-in-Return

    Nothing-in-Return

    Joined:
    Nov 21, 2013
    Posts:
    1
    This tutorial seems to be exactly what you're looking for:
     
    Westland likes this.