Sign in to follow this  
rogerdv

Problem instantiating objects

Recommended Posts

Im trying to use Instantiate() to equip an intem in the character model, the object is created in the expected point, but stays there instead of moving with the character mesh. This is my code:

GameObject weapon = Resources.Load("items/ancient-grtsword") as GameObject;
Instantiate(weapon,t.transform.position, Quaternion.identity); 
weapon.transform.SetParent(t.transform);
weapon.transform.position = Vector3.zero;

t is a GameObject, assigned from the editor. I have tried both using the bone (the mesh has an specific bone to attach the item), and creating an empty GameObject in the bone and using it. How can I solve this?

 

Share this post


Link to post
Share on other sites

Try setting weapon.transform.localPosition to zero after the parent assignment.  See if that helps?

Also, have you looked in the scene hierarchy during runtime to make sure the parenting is attaching to the object you expect?  For example, is t still a child element of the character model?

Share this post


Link to post
Share on other sites

Thanks! using localPosition/Rotation solved my problems. Now Im looking some way to store a database with alol the required rotations/offsets, I thought I could handle that with the prefab.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this