Search code examples
c#unity-game-enginegameobject

How to draw on a 3D game object in Unity


I have a 3D cube (game object with material) in my Unity 3D application (C# scripts). If the user clicks the cube, I want a small red circle drawn on the cube at the exact mouse position where the user clicked. The cube's position may change as it can move around the board, but the circle should stay on the cube, at the proper position, like a sticker placed on it. Another way to think of it, pretend it's a character and they just got shot. The bullet hole follows that character as they move around.

What is the best way to achieve this in Unity? Ideally, once drawn, I wouldn't have to continue to recalculate the circles position as the cube moved around (as I imagine that will prove difficult over time). I just want it stuck to the cube, again like a sticker.

Note: At any given moment, there would only be 20 or so of these circles, so performance isn't a huge concern.


Solution

  • As mentioned in the comments above, using the transform parent handled this perfectly, in only one line of code:

    sphere.transform.parent = cube.transform;