How to set Instantiate position bound by parent's location
I am attempting to have a tile map generate and display on screen. However, whenever I attempt to Instantiate a given tile, instead of spawning the object within the bounds of the display area (a Viewport, in this case), the tiles spawn offset from 0,0 world position, rather than the Top-left corner of the display area.
The exact line of code that spawns the tiles is:
Instantiate(tile, new Vector3(x * scale, -1 * (y * scale), 0), Quaternion.identity, parent.transform);
Where "tile" is the actual map tile that I want shown, "x" and "y" are the coordinates of the tile, and parent is the Viewport's Transform, found with:
parent = GameObject.FindGameObjectWithTag("Map Display").GetComponent<Transform>();
The code as written gives me the following output:
Whereas the output I want is:
What am I doing wrong/how do I fix this?
Answer by ahstein · Oct 28, 2018 at 05:34 PM
Have you tried using cameraToWorldMatrix? https://docs.unity3d.com/ScriptReference/Camera-cameraToWorldMatrix.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612181118im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to create a 2d (javascript like) image map in unity 2d 0 Answers
How to display text from a text object to another text object? 1 Answer
Canvas behaves differently on PC fullscreen and WebGL fullscreen 0 Answers
How to fit content into a specified area of screen at game startup 1 Answer
Reserve ammo not displaying on UI 0 Answers