There are a handful of ways to accomplish this, but I’m really looking to narrow it down to a particular efficient solution. If you have any ideas or suggestions, please let me know
Problem: In the canvas system, how would you attach objects (animated sprites), to the edge of a circle (we’ll say a single solid color perfect circle) in another sprite. You can assume the sprite is the only content and completely centered in this sprite (it is not a spritesheet). In particular, how would you do it through code, after instantiating the object from a prefab?
Answers that need work:
Use a circle collider and attach it to the canvas sprite (doesn’t scale)
Use a mesh collider like above, maybe a cylinder, or a custom 2D circle mesh (leaning towards this)
Read the pixels from the graphic and find the separation points (a lot more work, for likely no more result than #2)
Again, if you have any ideas to add to this I’ve overlooked, or suggestions on the existing solutions I’ve thought of, let me know. I appreciate it, thanks so much!
This.
You can certainly build a script that does it mathematically, but really using transforms and nesting is a great way to go and leverage existing features. Cheat as often as possible.
Well if you know the radius of the circle, the x,y points on it are defined by the sine and cosine. There are 2*pi radians in a whole circle and you can divide those equally among the number of sprites you want to stick to it. You can do something like this:
var numSprites = sprites.Count;
for(int i=0; i < numSprites; i++) {
var rot = 2f * Mathf.PI * i / numSprites;
var x = Mathf.Sin(rot) * radius;
var y = Mathf.Cos(rot) * radius;
sprites[i].SetParent(circleObject);
sprites[i].transform.position = new Vector2(x, y);
}
I have a couple of ideas to combine the pivot with some math, I’ll let you know how things work out in case anyone wants to create a similar control scheme.