- Home /
Answer by carrollh · Jan 18, 2015 at 02:46 AM
EDIT: after seeing @michael96schmidt's answer, it made me realize why I do it this way in my game. My weapons have rigidbodies and a lot of mass so they can bash things around. The more mass the thing has the further out of position it will get as you move around. His method is way better if you don't need rigidbodies on the weapons. You can achieve it in code with something like weapon.transform.SetParent(attachPoint.transform) /EDIT
The way I do it (which may be the naive way or the preferred way, I don't know), is in 2 parts:
1) After adding the character model to the scene, in the hierarchy I expand the joint tree for the character until I find where I want to put dynamic stuff (holsters, hands, etc). Then I add GameObjects to these joints, and then translate and rotate them into the correct position for what I need.
2) Then I attach a script to the weapon or object that looks like this:
using UnityEngine;
public class Weapon : MonoBehaviour {
public Transform attachPoint;
public GameObject owner;
void Start()
{
if (owner != null)
{
transform.localScale = owner.transform.localScale;
}
}
void Update()
{
if (owner != null)
{
transform.position = attachPoint.position;
transform.rotation = attachPoint.rotation;
}
else Destroy(gameObject);
}
3) Then I drag the GameObject I added to act as an attachment point or holster onto the scripts public Transform attachPoint in the Inspector.
Note: You might not need the Start() method or the Destroy call. For example you want the sword or whatever to stick around in scene after the owner dies or whatever.
Answer by michael96schmidt · Jan 18, 2015 at 05:54 AM
I just put an empty gameobject as a child of the hand/arm and offset it to be in the position of where the weapon would be. I then just have the equipped weapon a child of that gameobject.
This should totally work, as long as you don't have a rigidbody with any mass attached to the weapon. If you do, you'll have to update the weapon's position and rotation to stay put periodically.
Your answer
![](https://koobas.hobune.stream/wayback/20220613180732im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
enable/disable child objects? 2 Answers
How to Access Components in Children of GameObject? 1 Answer
change child texture problem 2 Answers
all parents of a transform 1 Answer
Set up rank children 1 Answer