Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting an object to the expected size

Discussion in 'Scripting' started by DerickTP, Jul 28, 2014.

  1. DerickTP

    DerickTP

    Joined:
    Jul 28, 2014
    Posts:
    8
    Hello everyone,

    I have seen this question a few times here and there. However, I haven't found an answer that works with my problem.

    Here it is: I am basically making a Tetris Game, and I have a Sprite Object that represents the array were the tetris pieces will fall. I want to size this element so that its height is a certain ratio of the camera height, and it has a certain number of lines and columns. Here is the code I've written so far (I hope it is readable, the preview is not so great):


    Code (CSharp):
    1.  
    2.     public class WorldCreate : MonoBehaviour {
    3.          // Size of the said array
    4.         public int lineNumber = 20;
    5.         public int columnNumber = 10;
    6.         float tileWidth = 0.32f;
    7.  
    8.         //ratio to set the world object to
    9.  
    10.         public float ratio = 0.8f;
    11.  
    12.         // Use this for initialization
    13.         void Start () {
    14.             //compute desired width, height and tilewidth
    15.             float height = 2 * Camera.main.orthographicSize; // height should 2 times the camera orthographic size am I right?
    16.             Debug.Log ("Camera height: " + Camera.main.orthographicSize + "\tObject height: " + height);
    17.             tileWidth = height / (float) lineNumber;
    18.             float width = columnNumber * tileWidth;
    19.  
    20.             //Compute the new scale in X and Y (simple cross multiplication)
    21.             float oldWidth = this.GetComponent<SpriteRenderer> ().sprite.bounds.size.x;
    22.             float oldheight = this.GetComponent<SpriteRenderer> ().sprite.bounds.size.y;
    23.             Vector3 oldLocalScale = transform.localScale;
    24.             float newScaleX = oldLocalScale.x * width / oldWidth;
    25.             float newScaleY = newScaleX*2;//oldLocalScale.y * height / oldheight;
    26.  
    27.             //Apply the transform
    28.             transform.localScale = new Vector3 (newScaleX, newScaleY,0);
    29.             Debug.Log ("Height: " + oldheight + "\tWidth: " + oldWidth + "\tTile width: " + tileWidth);
    30.             Debug.Log ("Height: " + height + "\tWidth: " + width + "\tTile width: " + tileWidth);
    31.             Debug.Log("OldLocalScale: " + oldLocalScale + "Local Scale: " + transform.localScale);
    32.         }
    33.      
    34.     }
    Now I use the computed tile width to set the base size of the tetris pieces, and I try to place it on the top left tile. The result is very strange. First, the array is way too big. Then, the number of columns is too small and the number of lines is too big, and finally, the real height and width of the world are wrong!!! The only thing that is right is the size of the tetris pieces, and when I move it to the right one column, it moves one column. I also tried to draw debug lines but the result was too strange...

    I don't understand how it is possible, I guess there is something I didn't understood somewhere. Any help woul dbe apreciated. Please don't hesistate to ask for more information...

    Thank you!
     
  2. DerickTP

    DerickTP

    Joined:
    Jul 28, 2014
    Posts:
    8
    Nevermind, I solved my own problem, using a more simple technique... I construct the world using an array of tiles that have the same width as the base tetris elements... stupid me :)

    EDIT: I don't know how to mark the thread solved...