V
V
Viktor Novikov2017-02-18 22:05:25
Unity
Viktor Novikov, 2017-02-18 22:05:25

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);
}

Contrary to expectations to see a sprite in the upper left corner, I watch it in the center of the screen ... Well ... OK.
Move on.
If I add another sprite and set its value yto 1
void Start () {
    Instantiate(star, new Vector3(0, 0, 0), Quaternion.identity);
    Instantiate(star, new Vector3(0, 1, 0), Quaternion.identity);
}

Then I’ll quite want to see it to the right of the first sprite by 1 pixel.
But no.
It will be to the left not by 1 pixel, but by 1 cell.
Further, I learned that the height and width of the camera are measured in these same "cells". Nuuuu. OK.
So, again: I want to draw a sprite in the top left corner of the screen.
The camera is measured in "cells".... So... We need to get the size of the camera...
According to the documentation, the size of the camera is a single number:
Camera.main.orthographicSize
Ok. In height, I have it, let's say 8. It Camera.main.orthographicSizestores this very 8-ku in itself.
What about the width? Where to get it? I can't find it... It changes depending on the height if I change it in the editor.
Apparently I'm not digging there? Wow.. let's say. Googling, I find this:
Instantiate(star, Camera.main.ScreenToWorldPoint(new Vector3(real_x, real_y, 1)), Quaternion.identity);

It looks more like the truth ... True, the origin of coordinates has shifted to the lower left corner, and not to the upper one, as I expected ...
In general ... Please tell me if I'm digging in the right steppe and how they work with coordinates in Unity 2D?
As a result, I want to make a toy like "3 in a row", where the field with cells is closer to the left edge of the screen.
Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Espleth, 2017-02-18
@YourDesire

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.

G
Georgy Pelageykin, 2017-02-19
@ArXen42

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 question

Ask a Question

731 491 924 answers to any question