Unity 2d Beginner Hints

Hi there pals! :smile:
I’m a rookie as a programmer, but I’d really like to improve my skills by confronting me with the subject itself.
That’s why I’m currently working as an amateur on a humble project which features 2d above all.

Unity seems an awesome engine, especially because it’s so much reliable and still manages to remain free… but seems also very 3d minded.
I like that, don’t misunderstand me, but right now I could use a couple of suggestions on how to handle it better in 2d.
Are there any useful expedients that I’m not aware of?

For example… the Transform.LookAt seems like a powrful tool, but it’ s built to " rotate the transform to point its up direction vector in the direction hinted at ".
This means that if I want to use such a tool in my script, I’ll have to handle all my game with X and Z axis, discarding the Y one.
I wasn’t doing like this at the beginning,but it seems to work fine, even if it’s kinda odd to handle.
To spare the player hours of pain understanding the inputs, I’ve rotated the camera and scripted those input to match the player’s perceptions.

Is there anything else I should be aware of?
Maybe a way to turn the textures applied to an object? Right now, if I want one of my drawings to face a specific direction, I have to rotate the object to wich I’ve applied it.

Thanks a lot for your contributions!

void Update() {
    float xRotate = Camera.main.transform.localRotation.eulerAngles.x;
    float yRotate = Camera.main.transform.localRotation.eulerAngles.y;
    float zRotate = transform.localRotation.eulerAngles.z;
    transform.localEulerAngles = new Vector3(xRotate, yRotate, zRotate);
}

This little code snippet placed in any of your components will cause that component to face the currently active camera. It assumes that your object is facing along the -Z axis. If it is facing along a different axis, simply adjust which of the axis are getting updated. This allows for any object in Unity to mimic the “billboarding” effect found in some games. It is very useful for making 3D elements appear to be 2D. I use it all the time with simple quads.

Hi RichardKain! I don’t know if I can put that to use in my case since my elements are already in 2d, but that sounds really cool anyway, I’m looking forward to try that out! Maybe on my background elements?! Thanks again!

The big difference is whether or not you are using an orthographic camera. If you are going the orthographic route, the code I posted won’t really be worth anything. Most games using an ortho camera never bother rotating the camera. With a perspective camera, this code has plenty of various applications.

One of the most famous applications of billboarding is in the original ID shooter series. (Wolfenstein 3D, Doom, etc…) These games used billboards for their enemy characters, and to “fake” a 3D world. Billboarding is also commonly used for particles effects.

I could go into how to use Unity’s scripting to cook up your own quads, but it is considerably more complex.

Isn’t it more efficient to set transform as a variable (var tr = transform; tr.localEurlerAngles ectect)?
I’d expect it would be more efficient to save Camera.main.transform regardless.

It would be more efficient if the script were more complex. As it stands, I only reference each of the two objects you are referring to twice. (Camera.main get’s referenced twice, and transform gets referenced twice) With such a small number of calls, there isn’t any real advantage to creating a variable to use as a shorter reference. Those four lines are the whole script, there isn’t anything else to it. I don’t bother trying to shorten code that is already so brief.

It’s more about CPU efficiency than “short code”. I wouldn’t bother caching transform if it’s just being called once or twice per Update; it would technically be slightly more efficient, but not really worth the bother. However caching Camera.main.transform might be worthwhile, so it doesn’t have to do the Camera.main lookup twice per frame.

–Eric