Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by leonsver1 · Jun 26, 2015 at 06:12 PM · camera2dprocedural generationsidescroller

how to get the lowest right corner of the camera(and highest left) in units?

hey guys i need help,im making a 2d procedurally generated sidescroller game and i want my "world" to be created from the lowest point of the camera to the highest point of the camera divided by 0.64 units cubed tiles and when i try Camera.main.Rect and use the minX to get the lowest point i get 0 and it shouldnt give me 0 because it is not 0..can anyone help with that please?

WHAT IT DOES: it loops on i as the index for horizontal block j is the index for vertical block the place to start the generation of blocks is supposed to be from the camera's lowest y point. the blocks are 0.64 units each but something was wrong with my calculation and it was rendered better if i calculated the space between each block as index times 0.62+offset that i tried getting and if i were to to index times 0.64 it would render it with black lines between my sprites.. if theres a fix for that too id really appreciate that. thank you for the solvers :)

my code:

   using UnityEngine;
     using UnityEngine.UI;
     using System.Collections;
     
     public class terrainGenScript : MonoBehaviour
     {
         public GameObject block;
         public GameObject dirtTop;
         public GameObject dirtMid;
         public GameObject dirtBot;
         public GameObject knife;
         public double offset;
         public Camera cam;
         public Canvas canvas;
         public Text myText;
     
         // Use this for initialization
         
         
         void Start()
         {
             
             offset = 0.016;
         }
     
         // Update is called once per frame
         void Update()
         {
     
         }
         public void Gen()
     {
     
             
             
             //sprite pixels / 100 and units in import setting have to be 100
             double sizeOfblock = 0.62;
          
                Rect r = Camera.main.rect;
 //number of blocks fitting in the height of the cam
                float numOfFittingBlocks = (Camera.main.orthographicSize*2) / 0.64f;
     
                int numOfBlocksHeight = (int)(numOfFittingBlocks);
                if (numOfFittingBlocks - numOfBlocksHeight >= 0.5)
                    numOfBlocksHeight++;
     
     
             //number of blocks on the x axis
             int numOfblocks = Random.Range(64, 120);
             Debug.Log(numOfblocks+"numOfBlocks on x axis");
             
             //just 
             //int numOfBlocksHeight = (int)(numOfFittingBlocks);
     
             //2d array represents actual world blocks-the world itself ingame
             GameObject[,] arrayOfblocks = new GameObject[numOfBlocksHeight, numOfblocks];
     
     
             
             //Vector2 firstPos = new Vector2(0f, 4.3f);
             //array of block types
             GameObject[] blockTypes = new GameObject[5];
             //assigning
             blockTypes[0] = dirtTop;
             blockTypes[1] = dirtMid;
             blockTypes[2] = dirtBot;
             blockTypes[3] = knife;
             blockTypes[4] = block;
 
 ////////////////////////////////////////////////////////////////////
             r = Camera.main.rect;
             float camerasLowestY = r.yMin;
 ///////////////////////////////////////////////////    
             myText.text = "r.Height="+r.height+"lowest y"+camerasLowestY+ "numOfBlocksHeight on y asix"+numOfBlocksHeight;
      
     
     
             Debug.Log(numOfBlocksHeight+"numOfBlocksHeight on y axis");
             //actual building
             for (int i = 0; i < numOfBlocksHeight; i++)
             {
                  for (int j = 0; j < numOfblocks; j++)
                     {
                      
                      if(i<3 || i> numOfBlocksHeight-4)
                    arrayOfblocks[i, j] = (GameObject)(Instantiate(blockTypes[1], new Vector3((float)((j * (sizeOfblock+offset))), (float)((camerasLowestY + i * (sizeOfblock + offset))), 0f), new Quaternion(0, 0, 0, 0)));
                   
     
                     }
               
             }
         }
         public float GetScale(GameObject t)
         {
             float locs = t.GetComponent<Transform>().localScale.x;
             Debug.Log((double)(locs)+"scale");
             return locs;
             
         }
     
     
     }
     


Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image leonsver1 · Jun 26, 2015 at 09:15 PM 0
Share

alright thank you ill try to rephrase that basiclly i have two loops one that runs on the vertical blocks and the other on vertical,basiclly i want to save memory by making the map only generate at the start of the game from the bottom of the camera up and just not more than that,because of that i want to get the screen height and count the number of blocks that can fit on screen which are the size of 0.64 units so i want to calculate screen height/0.64. and when i tried the methods above it didn't really work... its kinda hard to explain but thank you i think the links would help a lot. thank you :).

avatar image leonsver1 · Jun 26, 2015 at 09:31 PM 0
Share

just checked viewPortToWorldPoint and thats exactly what i need thank you so much!

avatar image Hellium · Jun 26, 2015 at 09:36 PM 0
Share

Nice ;) I have switched my comment to an answer. Please, click on the check mark under the vote button to accept the answer. Somebody could face a similar problem in the future.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Hellium · Jun 26, 2015 at 06:28 PM

"the lowest point of the camera to the highest point of the camera divided by 0.64 units"

This doesn't mean anything.

I don't really understand what is your question.

The Screen class can give you the size of your screen http://docs.unity3d.com/ScriptReference/Screen.html

Camera.main.ViewportToWorldPoint can give you a 3D/2D position from a point in the view port (between 0 and 1), and Camera.main.ScreenToWorldPoint does the same but with a screen space point (between 0 and Screen.widt or Screen.height) http://docs.unity3d.com/ScriptReference/Camera.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2.5D Platformer Camera follow character Y axis but not for every jump? 1 Answer

Position Gui Text Universally 1 Answer

Diagonal camera limit 0 Answers

2D Animation does not start 1 Answer

Passing Underneath Section of a Mesh 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges