Search Unity

break joint -> unparent object -> addforce(Forcemode.Impulse) not applying force

Discussion in 'Physics' started by Tarball, Apr 26, 2017.

  1. Tarball

    Tarball

    Joined:
    Oct 16, 2016
    Posts:
    166
    Here is the function:

    Code (CSharp):
    1.     private void shoot_arrow()
    2.     {
    3.         if (player_script.bow_fire == true && out_of_arrows == false)
    4.         {
    5.             break_joint = true;
    6.             arrows_to_shoot[arrow_index].transform.parent = null;
    7.             Vector3 flight_direction = -transform.up.normalized;
    8.             arrows_to_shoot[arrow_index].AddForce(1.4f * flight_direction, ForceMode.Impulse);
    9.             arrow_index = arrow_index - 1;
    10.             player_script.bow_fire = false;
    11.  
    12.  
    13.         }
    14.     }
    The function breaks the joint and deparents the arrow from the bow, but the arrow just falls to the ground. Before you tell me to add more force, I've tried that, and it still doesn't work. Units of ForceMode.Impulse are units of momentum, according to the scripting API (not Newtons, in accordance with physics). My arrow has mass of 0.02 kg, so this should give 1.4/0.02 = 70 m/s initial velocity, applied instantly, which is the desired outcome. IsKinematic is not checked.

    Why won't it apply the initial velocity? What have I done wrong? Any help would be greatly appreciated.

    Edit: If I comment out the change in arrow index and fire again, it shoots along the ground. Is there some issue with breaking the joint/deparenting that causes the force (momentum) to not be applied the first time?
     
    Last edited: Apr 26, 2017
  2. Tarball

    Tarball

    Joined:
    Oct 16, 2016
    Posts:
    166
    So, it turns out that breaking the joint in another function called by Update() is a bad idea. Changing that fixed the problem.