- Home /
How to define a point on a sprite, and get that point in world space.
What I want is to be able to define a series of points on a sprite that can function as an anchor for modular pieces- like if I have several heads, hairs, eyes, nose, lips, ect.. If I could define a point on the head sprite where the eye would go, and then another point on the eye sprite where I wanted it to attach, I could just position the eye so that it's anchor lines up with the "eye socket" anchor point on the head. Can anybody point me in the right direction? Thanks!!
Answer by Dblfstr · Mar 20, 2014 at 06:46 PM
Yep, for every item that you want to place (i.e. eye, nose, lips, etc) Create an empty gameobject at that location. For instance create an empty gameobject and position it at the eye socket. Name this gameobject "eyeLocation" or "eyeLocationLeft" or something like that.
Now, how you decide to apply the eye in this location will differ, adn can be done many ways. One way, is to have a script with an array of sprites Eye[]. Then, depending on what eye is chose, set the gameobjects Spriterenderer.sprite to Eye[1], or whichever eye index you desire.
There's no way of doing this that doesn't involve having to go through every sprite in every spritesheet and making a prefab out of it?
You want 'anchor points' on a sprite, that you would setup yourself. Well, these 'anchor points' are empty gameobjects. The gameobject simply defines a point in space. What you want to do at that particular point is up to you. I mean, I guess your head sprite (or any sprite which would have other sprites attached) would be a prefab combined with the gameObject anchors.
There are always paid assets like 2D Sprite kit, that may assist you.
However, for items that will not have other items attached, there is no need for a prefab. Also, you can change the pivot point of each sprite in the sprite editor. If you have a sprite sheet of multiple sprites, the little blue circle at the center of each sprite is movable, and is the pivot point (relative 0,0).
Can I ask the purpose of this functionality? Is if for creating an avatar for a player, or customizing a player. I am picturing $$anonymous$$r. Potato Head as I type...
sure- I'm working on a tool that procedurally generates assets given modular pieces from a spritesheet. $$anonymous$$r. potato head isn't really far off! The idea is to take a lot of busywork out of making varied characters/items/weapons/whatever. The issue is that when I swap out sprites that are different sizes and shapes from each other, they aren't positioned quite right anymore. (see before and after pics below.) So I need some kind of data that will give me a reference point to where things are supposed to match up. But it's not preferable to ask the user to go through and prepare every sprite with gameobjects in order to keep that info. I'm now thinking that I'll make a class that both stores the sprite, and an array of Vector2s that keep points on the sprite.rect, then somehow get those points in world space. The thing is, I'd have to rig up a way of visually representing those points in some sort of preview window or something. It's a lot of work, but I guess it is what it is. I am more than open to suggestions on better ways to do this- I have a tendency to over-complicate things.
Pics aren't showing up for me right now. But, are these going to be all your own sprites? or allow users to use whatever sprites they want? If they are all yours, it will be easier, since you can predefine everything you need. Otherwise, I don't know how you would achieve this (without the user having to set the reference points themselves. That, my friend, is way above my level. :D
Edit: Pics are showing.
Your answer
![](https://koobas.hobune.stream/wayback/20220613140741im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Resize a sprite by giving it more transparent pixels 0 Answers
My sprite from sprite.create looks pixelated. 1 Answer
Change Color Based On Pixel X Coordinate in Shader 1 Answer
Struggling to find how to access the Texture 2D of a sprite. 1 Answer
How do I access a texture slice's rect via scripting? 1 Answer