# Change parent object on instantiation

I'm making a game in which I have different weapons. One of them is a hand grenade. Well, the question is:

I made an animation with mecanim (I don't know if is the correct way) with the grenade being is a child of the main character. Then, the animation is correct (the grenade goes to high scale and I have the false sensation that is going up, to the camera).

The problem is that this grenade is attached to the character, and when I move the character, the grenade moves with him. I need the grenade to not be linked to the character when it's launched. And when I instantiate a new grenade, I need this animation playing.

I don't know if I explained the situation well, but this is the idea, an animated grenade launching when the player press the shoot button... what is the best way to do this?

For me it should be done in this way:

1. What we need:

• A throw point - a Transform attached to mode's palm of hand
• Throw animation
• (Option A ) A event that will be fired during the throw animation, at the moment that the grenade should leave player's hand / (Option B) Or the infomation about how much time it will take from the beginning of the throw animation to the moment when grenade should leave.
2. How we do it:

Option A:
In the function (let's call it ThrowGrenade, in class Player), you play Player's throw animation. Make another function called OnGrenadeLeave which will be called from animation (information about how to set up an animation event should be easily found on google ). In this function, the first thing we do is detach the grenade from Player's hand, just use grenade's transform.SetParrent() so set new parent for it (just simple null, it will not have parent object). Next we play animation on the grenade.

Option B
We declare function ThrowGrenade as a coroutine, which will do the followings: play Player's throw animation -> WaitForSeconds (timeTillGrenadeLeaves) -> detach grenade -> play grenade's animation.

In game, you just call ThrowGrenade, and that's it.

• Ok, It worked partially. I made 3 different types of shoot, one to up, another to medium distance and other to higher distance. I configured the position in animator, and the animation plays good on animator, but when I run the game, the grenade appears at 0, 0 position, not on the desired point (the shooting transform). I saw that if I quit the position configuration on animator, and stay only with scale, the grenade appears in the correct positon, but, when I put the position on the animation, the grenade appears at the end of the universe xD. What I can do now?? – Jordi4321 Sep 23 '16 at 14:30
• It depends if you let your animation control the position of the grenade or not, sometimes its better to control grenade's position by script, then when you have to deal with collisions or other problems with interaction it would be much more easier. With that being said, if you still want to control grenade's position via animation, then you must make a parent object for the grenade. The reason why it appears at 0,0 is that the position passed from the animation is relative, when the grenade has no parent and its position in first animation frame is 0,0 then this happens. – tc07 Sep 23 '16 at 14:46
• To overcome this, you must create a parent object for the grenade, and we will have a hierachy : throw point -> parent of grenade -> grenade (with animation ) , the parent of grenade at the moment when player throw should be detached from player's hand and acts as an origin to the grenade, then with the 0,0 from animation, the grenade will appear where its parent is. Next, with the parent's position unchanged, the rest of animation should play as expected – tc07 Sep 23 '16 at 14:50
• Oh!! Ok!! Looks good. I will make the animation control the grenade's position, becaouse I think, then, with collision, I need only put the colider active at the last (or near) frame of the animation, exactly when "touch" the floor (or nearly)...I thinking at this point execute an event that calls "collider active" function)..I don't know if is the best way but seems the easier. – Jordi4321 Sep 23 '16 at 16:10
• Incredible!! IT WORKED!! A lot of lot of thanks, you are the best!! At the moment that's all, later I will make the collisions. I go paste the launching part code of the script here. Thank you to all the community, as usual!!! – Jordi4321 Sep 23 '16 at 16:53

This is the code I used to make the shoot of the grenade. It works, but now, I'm thinking when I need to destroy the object on collision, is possible that I will need to put de newShoot variable out of the function, or create an array or something similar. I will think this problem on few days.

function ShootGrenade()
{
var newShoot : GameObject;