5
\$\begingroup\$

So, here is what I am trying to do in Unity 5 (using C#). I want to draw a a 2D circle at the background of a 3D scene and apply a texture/image to fill it.

Just like it would be if the circle was in the GUI, but now being always behind everything else instead of always in front (think of a moon in the sky of a piloting game, or a distant planet in a space game, etc).

What would be the fastest way to draw such a circle and, of course, update its radius depending on the camera distance? Of course, I don't want to use a sphere, I want to learn how to that with a simple circle that gets bigger or smaller depending on the distance of the viewer.

\$\endgroup\$
  • \$\begingroup\$ Did you try to setting radius according to the distance between camera and moon. (Vector3.distance)? \$\endgroup\$ – Hamza Hasan Dec 14 '15 at 19:29
  • \$\begingroup\$ You may want to use OnPreRender() to immediately draw your (masked) texture. I think that will achieve the effect you want. Then the scene will draw on top. However the best idea might be to draw the whatever to a texture and that texture is then used as the skybox. \$\endgroup\$ – Draco18s no longer trusts SE Jan 27 '16 at 21:03
  • \$\begingroup\$ @Draco18s the first suggestions seems promising. The problem with your second is that it would become impossible to rescale the circle without scaling all things that are draw in the skybox, right? Or are you suggesting run-time redrawing the skybox's texture? \$\endgroup\$ – Louis15 Jan 29 '16 at 1:25
  • \$\begingroup\$ @Louis15 No no, the skybox is a dynamic texture (a render texture). It gets redrawn every frame. \$\endgroup\$ – Draco18s no longer trusts SE Jan 29 '16 at 15:07
1
\$\begingroup\$

Ok, so just of the top of my head your going to want to make a new circle(2d) and apply a texture to this then write something along these lines (and attach it to the object)

Pseudo code:

private gameobject player
private float scale;

update{
face the player (using transform.lookat(player))
float distance = vector3.distance(transfrom.position,player.transform.position);
transform.scale = distance / scale;
}

The scale in this case changes as the player gets closer or further away using the global scale variable to change the sensitivity of it. Obviously i haven't tested it but in 5 min this would be my initial solution.

If you wanted to make it more efficient, then only face the player every few updates, possibly only make it call recursively every few seconds (depending how far away the player is/ how fast the player is).

An answer is better than none... :)

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Many thanks. What you are suggesting is the creation of a circle sprite that could be just put at the very far background and updated in terms of scale and rotation as needed? \$\endgroup\$ – Louis15 Jan 29 '16 at 1:28
  • \$\begingroup\$ yep, fairly simple. Alternatively you could have it be a fixed distance away from the camera but then you might run into clipping problems as you move around... \$\endgroup\$ – user3797758 Jan 29 '16 at 18:29
0
\$\begingroup\$

You probably need a skybox or a skydome. A skybox is a box around the camera, and is always centered around it. Basically a model, wich isn't get transformed by the view matrix.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ I know skyboxes, but how exactly that could help me doing what I've asked, i.e. having a circle at the background whose size can be updated as described? \$\endgroup\$ – Louis15 Jan 29 '16 at 1:26
  • \$\begingroup\$ Dinamically generated textures? \$\endgroup\$ – Bálint Jan 29 '16 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.