Unity Inventory : Attach Weapon To Player's Hand

preview_player
Показать описание
In this Unity tutorial I show you how to attach a picked up weapon or also other items to the hand of the player and align it correctly according to position and rotation by write C# code.

See my social profiles here

Рекомендации по теме
Комментарии
Автор

Perfect! Just what I was looking for!

BTW: you can create additional bone in a model for held item. And use it as parent. So you won't need to manualy set the offset for it in Unity. And you can do really cool animation with it (like juggling).

ИбрагимАбунай
Автор

Just found this. It's amazing how simple this is and yet how much it helped. No stupid intro, no random bullshit, you literally taught me an entire class worth of knowledge in like 50 seconds. King.

blaketev
Автор

your voice is so good almost made me forgot the purpose i came here for

자방랑-ks
Автор

Finally an approach without setting every possible item as a child object of the player model and disabling them while they're not in use... Thanks so much, have been looking for such an elegant solution for quite some time!

Tarrokk
Автор

Clear tutorial! This looks a lot simpler than I thought it would be.

jacobras
Автор

Man, you deserver a loto of more subs, every time i need some help in unity i find your videos!!! awesome

Enorday
Автор

after 10 years i find a way to attached the weapon to the fps arm. very helpful thks a lot

emre
Автор

Crucial step is missing here. Where does the "Hand" variable come from in your PlayerController script? You just casually reference Hand.transform.position

DragonSlave
Автор

Great tutorial
It's useful video Thankyou

musicland
Автор

No one else does this tutorial simply, this is great, thanks!

garywilkerson
Автор

Short, sweet, nice and simple. Very good!

Coolguywithmask
Автор

Exactly what I was searching for, thanks!

GaetsKrop
Автор

Wow that's wicked simple, clever work!

CowboyCoder
Автор

I cant find the specific bones and therefore are unable to parent the weapon to any of them.Plz help me!!!

l.vazakas
Автор

Or just add an empty on the bone that is called weapon holder and then you don't need to remember\hard code the coordinates.

HAWXLEADER
Автор

Thanks had trouble with where to parent it, and that helped me.

GabdaRocur
Автор

Yeah! great tutorial. I learned more than one thing! :D thank you for the good explanation!

darkrist
Автор

You may not want to parent/child weapons to the character skeleton at all in a real game. If there's any changes to scale for the weapon or the skeleton (for example if a monster powers up and grows) it will wreak havoc on things and your weapon will look screwed up. It's also potentially inefficient depending on how complex the hierarchy is: according to Unity's advice on optimization, updating lots of transforms in a complex hierarchy every frame can be taxing on performance, especially if you do it multiple times for different things or if you update transforms one by one like transform.position = ...; transform.rotation = ...; and every time you do that Unity has to fire an internal transformChanged event all the way up the hierarchy. Instead, extract the position and rotation, do your operations on it, then set position and rotation all at once. What's worse, they say adding child transforms is even worse, lol, and they recommend that you try to avoid pararenting things just to have a neat hierarchy in release builds because players/users can't see the hierarchy when you release your game and don't care how neat it was, so any objects you spawn or activate at runtime don't need to be reparented unless they _have_ to be to get the behavior you need. In this case, childing the weapon is a potentially valid use of parent/child relationships but just want people to be aware it _could_ be problematic in big games and cause some weird issues or slight performance decreases each time you do it. Especially matters if, for example, you have 200 soldiers fighting on a battlefield in a strategy game. Choose wisely based on what you're doing.

For attaching your weapons without parenting consider using a special attachment script in a big game that has translation and rotation offset fields for each weapon (determines how to adjust it to fit in character hand) and setting the weapon to the right place. Special care has to be taken for _update order_ of scripts or your weapon could be doing hit calculations and other things one frame behind (may not matter to your game though). I would update my weapon slots on hands after the animator updates and IK is applied, ensuring it moves to fit the next frame's hand position.

Speaking of IK, I just started really getting into Unity's Animation Rigging tools and it is _extremely_ powerful. You can make characters accurately touch/hit things, block incoming attacks, activate buttons and open doors, look around with their actual head and eyes instead of turning the entire body (great for AI), place feet accurately on terrain/floors with no clipping, hold and aim guns or bow and arrow, the possibilities are endless. It's a kind of "advanced" topic but it's well worth learning about to make games with characters that move and behave in a smart manner.

GameDevNerd
Автор

Hello men, I love your tutorials, for the next one, can you do for when you pick up an item a message appears that says what and how many did you collect?

Jota_GZ
Автор

h bro, nice tut, I have a weird question ... is it possible with "metarig" when my exporting setting from blender are actions strip and baked NLA ? I mean I export my fbx to unity, with my metarig (rigify but not pitchypoy.) ..and the animations too, I need the axe as separate objetc... I know how to fix with another way...but is most complicated...this way is easiest one...but In my case I ve never make the solution...because my metarig is not in the inspector, all my metarig ar actions in my project, I can only see the metarig in avatar setting...I do not how to see my metarig in my hierachy...?? any solution to see the bones in hierachy? to fix it ? and use this easy way ? thx.

leatherandpoemscharmyman