Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Best Approach For Static Background Image?

Discussion in 'Scripting' started by Payaso_Prince, Jan 19, 2023.

  1. Payaso_Prince

    Payaso_Prince

    Joined:
    Jul 17, 2017
    Posts:
    85
    I apologize if this is posted in the wrong forum. I honestly was not sure which section would be best.

    I want to have a static background image of a sky that stays completely flat on screen and perfectly fits my games 1920 x 1080 display. Similar to what most old 2D side scrollers did. In other words, I don't want to simply drag an image of the sky backward on the Z-axis in 3D space.

    Originally, what I was doing to accomplish this was having a 2D "base" camera solely dedicated to rendering this sky image behind everything. I then used a a separate 3D "Overlay" camera to render everything else on top of it.

    Here's an example, the blocked out sky section would be the base 2D camera and everything else on screen would be the 3D overlay camera:

    This accomplished exactly what I needed, but I think having my base camera only render the sky, and having an overlay camera render everything else is a bad idea. I cannot think of another way that works while avoiding pulling the image back in the Z- axis (thus making the image no longer fit the screen size perfectly).

    Does anyone have a better way of accomplishing this? Thanks so much!
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Why was it a bad idea?

    As for the z access for the background, that's probably what I would normally do. My sky image wouldn't be perfectly sized, I'd probably have it slightly larger so it will still take up the entire screen size.
     
  3. CPU_USAGE

    CPU_USAGE

    Joined:
    Jan 15, 2023
    Posts:
    20
    Put your background image as a sprite in a Rect Transform, use the Rect to match the Rect of the Camera. Instead of modifying the Indominous Z Axis.

    A rect in great numbers is more costly than no rect. However for one component of the game, as is yours the sky box; a rect is no trouble at all. Just a one liner to match the camera rect scale. Should be aligned as it follows. If camera zoom out, the rect would have to scale to match camera view rect.

    The purpose of setting the rect transforms Size value is so that an entire camera didn't need to render 1600x900 more pixels as a separate image and combine them so you are right in thinking its a stupid solution.
     
    Payaso_Prince likes this.
  4. Payaso_Prince

    Payaso_Prince

    Joined:
    Jul 17, 2017
    Posts:
    85
    Hey thanks for the response!
    I just wanted to clarify a few things:

    1. When you say "Put your background image as a sprite in a Rect Transform", you mean as a UI element that would go in the canvas? I'm under the impression that is what Rect Transforms are used for as opposed to a regular transform.

    2. When you say "use the Rect to match the Rect of the camera", can you elaborate on this a bit? I don't quite understand what you mean. Are you saying to make its scale the same size as the screen? If so, wouldn't it just cover up everything else on screen?
     
  5. CPU_USAGE

    CPU_USAGE

    Joined:
    Jan 15, 2023
    Posts:
    20
    1. I mean as a world space canvas my friend. Thats on canvas setting -> World Space

    2. GetComponent<RectTransform>().Size Unity - Scripting API: RectTransform.sizeDelta (unity3d.com) should be the same as the camera rect. The camera should center this rect have this rect be child of camera, and match its rect size.


    - Create a GameObject
    -Add a Canvas
    -Set to World Space
    -Set its Size and Position to match the camera.
    -Anchor your sprite sky inside, as a child, or as an image attached directly to the canvs.
     
  6. Payaso_Prince

    Payaso_Prince

    Joined:
    Jul 17, 2017
    Posts:
    85
    Sure, I can do this. But the sky sprite is covering all of my 3D models now.
    Are you saying to also drag it behind everything on the Z-axis?
     
  7. CPU_USAGE

    CPU_USAGE

    Joined:
    Jan 15, 2023
    Posts:
    20
    Yeah, same setup. So a world space canvas will be drawn behind what is infront of it.

    As you have the 2.5D i assume your original scaling sprite was in the background already. Scale is less accurate than Rect transform size, will require deeper mathematics to align correctly. Or ratio guestimate. Rect is the way to go i would think.
     
    Payaso_Prince likes this.
  8. Payaso_Prince

    Payaso_Prince

    Joined:
    Jul 17, 2017
    Posts:
    85
    Okay, so even thought its not UI, you're saying to give it a Rect Transform, rather than a Transform, right?

    I'd like to try this. I have placed my sky sprite behind everything in the Z-axis, and I have made it a child of the MainCamera. How can I make it perfectly match the screen size now?
     
  9. CPU_USAGE

    CPU_USAGE

    Joined:
    Jan 15, 2023
    Posts:
    20
    WIDTH_HEGIHT.png

    This one contains an Image component for the sprite holder.


    The camera components default aspect ratio rect reading 0 0 means that it = to the defined screen resolution.


    So it is all to be = to your screen resolution. If its too small because of the Z, then you may have to make it = to screen resolution but 1.05x as large or something
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
  11. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    So why was using camera depth and culling mask a bad idea? FPS uses this all the time in order for the player hands to not clip through the environment. Damn sure games which need to render deep space use such perspective tricks to side step issues with portraying vast space as well.
     
    spiney199 likes this.
  12. Payaso_Prince

    Payaso_Prince

    Joined:
    Jul 17, 2017
    Posts:
    85
    Thanks for all the replies!

    It looks like there's quite a few ways to accomplish this. Using another camera does sound like a simpler route. Performance wise, is using a separate camera to accomplish this much more demanding?
    (I don't actually have any idea how resource intensive it is.)
     
  13. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    The culling mask is supposed to take care of that. You are splitting the rendering load between the cameras.
     
    Payaso_Prince likes this.