Answer the question
In order to leave comments, you need to log in
How to prepare coordinates in Unity 2D?
I've never played with things like Unity before. Perhaps the question is very stupid.
Hello.
I just can't figure out how to use coordinates in Unity 2D ( Orthographic Camera).
Created an empty 2D project, scene, script.
I want to programmatically draw an object in the upper left corner. OK. I created a prefab (prefab) based on a sprite and I write based on the official guide :
void Start () {
Instantiate(star, new Vector3(0, 0, 0), Quaternion.identity);
}
y
to 1void Start () {
Instantiate(star, new Vector3(0, 0, 0), Quaternion.identity);
Instantiate(star, new Vector3(0, 1, 0), Quaternion.identity);
}
Camera.main.orthographicSize
Camera.main.orthographicSize
stores this very 8-ku in itself. Instantiate(star, Camera.main.ScreenToWorldPoint(new Vector3(real_x, real_y, 1)), Quaternion.identity);
Answer the question
In order to leave comments, you need to log in
1) There are a bunch of tutorials for Unity, where everything is described in detail, it’s worth a little google, or even just go to the off site, the tutorials are not bad there. I strongly recommend starting with this, and not immediately into the code. For example , here or here will suit you.
2) This is not WinForms, Unity has such a thing as a scene. This is not what the camera renders: the camera can be moved as you like, objects on the scene do not change their location. And by default, your camera looks at the center of the scene, that is, in the center of the camera, the coordinates are (0; 0; 0). And everything is abstracted from pixels, how many pixels the object will move in your engine decides depending on the screen resolution and camera settings.
If you need to snap sprites to the edges of the screen, then perhaps you should use the UI system. Well, add a Canvas there, throw a UI sprite on it, you can select snapping to the edges of the screen, pivot, etc. for the sprite. etc.
If the UI is not at all what you need, then, apparently, it is worth digging into the connection of screen coordinates (tobish pixels) with world coordinates. Some kind of cameraToWorldMatrix maybe.
On the other hand, not so long ago, on a structurally similar Urho3D, without any problems, did something similar without any matrices. There, OrthographicSize was the vertical size of the camera in game coordinates (probably here too). From AspectRatio received accordingly and width. Well, knowing the coordinates and dimensions of the camera, it is not difficult to arrange everything as it should.
There I also used the fact that the dimensions of the 2D sprite in the game space were exactly 100 times smaller than the pixel ones, I didn’t have to delve into Unity3d, but it seems like reference pixels per unit is just about that.
But this is all for very specific cases.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question