Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 gerudoking · Sep 19, 2017 at 06:53 PM · cameragameobjectpositionscreen

Set game object position relative to the Camera

Hello,

I'm coding a script that reads a text file with a tilemap(like the ones from the tiled tool), and then creates game objects in the game world, each representing a tile. The thing is, after creating the game objects, I try to position them relative to top left corner of the screen, like in a grid. But whenever I run the game, the objects are positioned far from the camera, and with a great space between then. Here is the code who creates and set the position of the objects:

 void SetMap() {
         for (int i = 0; i < tileMatrix.Count(); i++) {
             if (tileMatrix[i] > -1) {
                 print(i);
                 map.Add(GameObject.Instantiate(tileObjects[tileMatrix[i]]) as GameObject);
                 map[i].transform.parent = gameObject.transform;
             }
             else {
                 map.Add(null);
             }
         }
 
         for(int i = 0; i < mapHeight; i++) {
             for(int j = 0; j < mapWidth; j++) {
                 print(j + i * mapWidth);
                 if(map[j + (i * mapWidth)] != null)
                     map[j + (i * mapWidth)].transform.localPosition = new Vector3(i * tileWidth, Screen.height - j * tileHeight);
             }
         }
     }

This code is in a object who is the parent of all tiles. Also, I'm using a canvas with "Screen Space - Camera", the game is supposed to be 2D topdown with the camera locked in a position, and the tile objects are prefabs with only 32x32 sprites.

It is my intention to use this code to create objects starting from the top left corner, each adjacent to the other.

After some research, I believe it might have something to do with the difference between units in the game world and pixels. Still, don't have a clue on which I'm actually using in this code, which to use for my intended purpose, or if this is actually the problem.

Any help is very well appreciated!

EDIT: The screenshots from the game view, the main camera inspector, and from the tile object(greentestblock) who is spawning outside the view:

alt text

alt text

help00.png (55.3 kB)
help01.png (28.7 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Sep 19, 2017 at 08:12 PM

Hello @gerudoking !

I don't understand what happens whith the objects, "away from the camera"? Can you post some screenshots to see it?

If the camera is immobile, you should be able to instantiate at concrete world position. You are doing this:

 Instantiate(tileObjects[tileMatrix[i]])

Why not instantiate giving the position?

 Instantiate(tileObjects[tileMatrix[i]], position, rotation)
Comment
Add comment · Show 1 · 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
avatar image gerudoking · Sep 20, 2017 at 02:31 PM 0
Share

Hey @tormentoarmagedoom , I posted some screenshots in the question. Here's another one (from the canvas) as I can't post more than two images:

alt text

Also, is there a difference between during the instantiation provide the position and provide it after the instantiation? I'm assigning the position later because of the logic I thought for positioning the objects.

help02.png (44.9 kB)

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

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Following a gameObject, unexpected problems? 1 Answer

Mouse.position from center of player 1 Answer

How to make the Health bar on Enemys head not be in relation to Player(main) Camera? 2 Answers

how to have an object positioned relative to the screen(like a button) 1 Answer

How can I get exact screen position of 3D game object in front of camera while using cardboard in VR game? 1 Answer


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