1
\$\begingroup\$

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?

\$\endgroup\$
0

2 Answers 2

3
\$\begingroup\$

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.

\$\endgroup\$
7
  • \$\begingroup\$ 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?? \$\endgroup\$
    – Jordi4321
    Sep 23, 2016 at 14:30
  • \$\begingroup\$ 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. \$\endgroup\$
    – tc07
    Sep 23, 2016 at 14:46
  • \$\begingroup\$ 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 \$\endgroup\$
    – tc07
    Sep 23, 2016 at 14:50
  • \$\begingroup\$ 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. \$\endgroup\$
    – Jordi4321
    Sep 23, 2016 at 16:10
  • \$\begingroup\$ 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!!! \$\endgroup\$
    – Jordi4321
    Sep 23, 2016 at 16:53
1
\$\begingroup\$

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;
  newShoot = Instantiate(grenade, lauchingPoint.transform);
  newShoot.transform.SetParent(null);
  newShoot.SetActive(true);
  newShoot.transform.FindChild("grenade_sprite").GetComponent.<Animator> ().Play("Grenade"+grenadeNumber);
}

"GrenadeNumber" is the number of animation to play: grenade1 shoots to up, 2 to front, and 3 to front but more far. The number is assigned pressing more or less time the shoot button. Grenade parent is deactivated, and I active it when I create a new one.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .