Carrying an axe… (Unity object positioning)

Positioning an item correctly to player’s hand can be a problem. Here’s one way to solve the issue. There won’t be much of code in this blog post. Everything done here is happening inside the Unity editor.

So, how do we from here:
attaching-item-axe-on-ground
an axe on ground… with physics

to here:
attaching-item-6
nicely positioned to player’s hand

The plan
The basic idea is that there will be an empty game object parented to the human mesh’s hand. When we pickup objects, we disable their collisions, attach the picked up object to the carry pivot, disable default item mesh and display alternative “picked up mode” mesh.

The “carry pivot” game object
“Carry pivot” is an empty gameobject that does nothing. Just sits there dumb on player’s hand. Here I’ve used a mesh renderer component so you can see where the pink ball is. That’s our carry pivot. That’s where things go.

I’ve simply opened the character’s bip skeleton, and located where his hand is. Often unity models seem to have something like this available — check out this.

attaching-item-carrypivot
pink ball marks the carrypivot on player’s hand

Attaching the axe
Once we have the pivot available, it’s time to attach our axe. When I pick up an object, I disable the collisions (or actually, in this case I simply switch the collision to be trigger instead of disabling the collisions).

Now that looks painful.

attaching-item-axe-physics
Auch…

Some code for attaching…
At this point, here’s some code for attaching the item. Feel free to skip this if you just want to follow the plan.

We either update item’s position on every frame, or we change its parent. I’m using “update every turn” for now, since that was the fastest way to try it out for multiplayer (for solo play, I was using the “parenting” method)

(Option A) the “update axe’s item every frame” method

function updateCarrying() { // what a silly name for a function

// "carryingPivot" is the game object to whcih we attach our items
_carryingGameobject.transform.position = this.carryingPivot.position;
_carryingGameobject.transform.rotation = this.carryingPivot.rotation;
}

Another way would be to change the parent of an object. I didn’t use this piece of code since I got into troubles with multiplayer code, and at this point it wasn’t important. Anyhow, either piece of code should be just fine.

(option B) if you don’t want to update every turn, then you can use something like this

// "attach once" -- call this when picking up an object
void attachToCarrypivot( GameObject childOb ) {
GameObject parentOb = this.carryingPivot.transform.gameObject;
childOb.transform.parent = parentOb.transform;
}

Back to the plan now…

Double-axe
I thought I could survive with 1 item that I simply attach to the hand… but this caused positioning problems. I would need to adjust the “carrypivot” in order to get an axe correct… and then what happens if I pick up a wooden branch? That would be positioned badly.

I decided to go with the following option: the axe game object has 2 meshes: one for “physical object” (that can be picked up, in the following picture it’s called “dropped_axe”) and one for the “picked up mode”. So, now it looked like here:

attaching-item-axe-physics-both
Here we have both of the axe meshes visible…

And then I simply enabled the “picked axe” object when it’s picked up, and disabled the “dropped axe”.

attaching-item-6
All done!

And there we have: an axe that works as a physical object in the game, but can be picked up. Naturally this means positioning object properly in the editor.

If I had any modeling skills, I could try edit the game object’s mesh and add there some sort of “carry pivot”, which would eliminate the need for 2 models. But since I don’t have modeling skills, this trick will do – at least for a prototype.

And it works with any asset store objects.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Pro-Human Quiz: