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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

2D Game Sprite-Camera issue

Discussion in 'Scripting' started by unity_IdF3Y7NrR21HSw, Apr 29, 2018.

  1. unity_IdF3Y7NrR21HSw

    unity_IdF3Y7NrR21HSw

    Joined:
    Apr 3, 2018
    Posts:
    32
    I`m a young Unity noob trying to complete a 2D game, I got a grasp on how the Canvas and scale with screen size works and it`s all good, but then for my 2d game I have a sprite which has a 1280x720 resolution and a 100 pixel perfect (not sure what this does), my game is meant to be played in landscape mode, all works well but then when I test the game on different resolutions the background Sprite does not scale with the screen size because it is not placed in a canvas and placing it in a canvas doesn`t make much sense to me and I`m pretty sure I`m not even supposed to from the various tutorials I`ve watched. When I change the resolution to a smaller one for example I no longer see the whole background and I have to go to the game scene and resize it again for the new main camera size. I resize it but then when I switch back to the bigger resolution I have blue bars on the sides and need to resize again. So is there any script that may constantly get the main camera size and resize the background sprite according to it whenever the camera resolution changes?? Keep in mind that I`m literally as dumb as a rock so please keep it simple.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I found a script online that does this. I modified it a little bit.
    I tested it and it seems to work, and if you are only in landscaped mode maybe all will be well for you. In my quick test, portrait turned out pretty bad. :)
    Anyways, you can try it and see. Just place this code on your sprite, and drag & drop the camera onto it. This script assumes an orthographic camera.
    Code (csharp):
    1. public class Test6 : MonoBehaviour {
    2.  
    3.     public Camera mainCam;
    4.  
    5.     void Start()
    6.     {
    7.         ResizeSpriteToScreen();
    8.     }
    9.     void ResizeSpriteToScreen()
    10.     {
    11.         var sr = GetComponent<SpriteRenderer>();
    12.         if (sr == null) return;
    13.  
    14.         transform.localScale = Vector3.one;
    15.  
    16.         var width = sr.sprite.bounds.size.x;
    17.         var height = sr.sprite.bounds.size.y;
    18.  
    19.         var worldScreenHeight = mainCam.orthographicSize * 2.0f;
    20.         var worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
    21.  
    22.         float sw = worldScreenWidth / width;
    23.         float sh = worldScreenHeight / height;
    24.         transform.localScale = new Vector3(sw, sh, 1);
    25.     }
    26. }
    This code also only runs once, of course. So, if the window is resizable, you'd have to account for that. First see if it's even doing what you'd like and doing it well. :)
     
  3. unity_IdF3Y7NrR21HSw

    unity_IdF3Y7NrR21HSw

    Joined:
    Apr 3, 2018
    Posts:
    32
    It works like a charm! God bless you.
    This thing was legit the biggest drawback for me I had no idea how to get around it, didn`t expect a simple script to fix it, God I love programming languages.

    Why didn`t I find this in pretty much any 2D developement tutorial?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm glad it worked for you. :)
     
  5. unity_IdF3Y7NrR21HSw

    unity_IdF3Y7NrR21HSw

    Joined:
    Apr 3, 2018
    Posts:
    32
    Wait, I`m sorry to bother you again but I also have a 2d empty sprite that acts as the spawner any chance you can help me find an easy way to have it somehow anchored to the background sprite and rescale at the same time with the background sprite?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hmm. Can you explain that any differently? If you rescale a spawner that is an empty game object what would be the difference? Are the spawned objects instantiated as children or something?

    As for anchored -- did you mean as a child or do you mean a centre position (or both)?
     
  7. unity_IdF3Y7NrR21HSw

    unity_IdF3Y7NrR21HSw

    Joined:
    Apr 3, 2018
    Posts:
    32
    Thanks for replying, let me elaborate, I have an empty 2d sprite that`s on the edge of my background sprite so it looks like numbers come into the background and slide along a road, if the background resizes after the camera the sprite that was on the edge is now somewhere else, farther away from the edge of the resized sprite, I want the spawner sprite to move at the same time with the background when the background gets resized.
    Could send a few pics if I still didn`t make myself clear enough.
    Basically when the background sprite gets slightly resized for all the portrait resolutions I also want the spawner sprite to move with the background.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I see. Probably doesn't even need to be a sprite, but either way.. You can try just setting it as a child and see if that works out. I did a quick test with a bad sprite to test so it's hard for me to be totally sure (looks okay in my test).
     
  9. unity_IdF3Y7NrR21HSw

    unity_IdF3Y7NrR21HSw

    Joined:
    Apr 3, 2018
    Posts:
    32
    I can`t believe it works, thank you so much dude, I`m so sorry to bother you with such stupid questions, much appreciated buddy, have a wonderful day.
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Np, take it easy. :)