- Home /
Placing an object
Let's say I have a game, where you build structures. Each gameobject can be in one of three positions:
In your hand (being carried) Lying on the ground Being set upright
For example: A log. When you carry it, its side rests in your palm. When lying on the ground, its side rests against the ground. When set in the ground (planted, like a pole), its pivot needs to be in the center of the log, a little ways from the bottom.
A sword. In your hand, it is vertically flat, and points forward. On the ground, it is lying on its side (90 degrees rotation from when it is in your hand), and if (for whatever reason) you wanted to stick it in the ground, its pivot would be in inside the sword, near the blade tip.
So, each object needs three different pivots and rotations that it needs to be set at, depending on whether or not it is being carried, on the ground, or planted.
Any ideas on how to approach this? My current approach is to have this:
Post
Hand
Ground
Planted
Each child would have its position and rotations set to position the object correctly. The gameobject would be attached to the appropriate child. The Post parent would just be placed on the ground or attached to your hand. The child objects would automatically handle rotation and adjustment.
Any thoughts on this?
looks good. Your post has 3 states it can be in. Each state would have code to reach that state.
The annoying thing would be the transitions possibly. you'd need an animation to transition so an object in your hand is moved to sticking in the ground and then relased from being a child.
an object on the ground needs a pickup transition. an object stuck in the grounds need an uproot transition.
Actually, I wasn't planning on there being an animation transition...but it might be a good idea. I was mostly concerned if it was a bad approach, or that maybe there was a better way. I think if I do have transitions, iTween would be fine :)
well you dont have to have transitions it just breaks immersion some to see objects magically transit.
That's some polish I think it would be good to have, eventually. I think ideally, I would have animations for the character that show them dropping something, picking it up, planting it, etc.
Answer by CrucibleLab · Nov 16, 2012 at 07:33 PM
Not sure whether you solved this problem already but just to add my thoughts:
Granted there's probably a better approach out there, my suggestion to you would be to have a separate 'parent' mount point for each of the three states; namely, in-hand, on-ground, planted.
Start by creating an empty GameObject (let me call it the 'MountPoint') and giving it an meaningful name for the given state. For the 'in-hand' state, I'd say you need to make the Mount Point a child of the left/right hand that will hold the sword. Then you would make the sword object to be the child of the in-hand MountPoint.
What I'd do to ensure the accurate positioning of the sword in character's hand is to drag both the character object and sword object into the Scene and move the sword in the palm of your character before applying the changes to the prefab.
A similar method can be used for the other two states. Again, I am only assuming here that you'd want your sword to be located at a specific spot on your map and that's where you would need to place your MountPoint. Once the sword is assigned to the MountPoint as a child, you would only need to modify the location and/or rotation of the MountPoint to give the sword a visibly different orientation (e.g., lying flat on the ground vs. "planted" on the ground).
Again, probably not the smartest way to do this but it worked for me. I hope it helps. :)
This approach seems to work very well. $$anonymous$$y prefab actually has three empty gameobjects as children (hand, ground, planted). Depending on what orientation I want the object to have, I just place it under the appropriate child. It's very easy to use the editor to position each one :)