Search Unity

AddRelativeForce not working

Discussion in 'Scripting' started by FisherM, Aug 28, 2014.

  1. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    So I try to apply a force here
    Code (CSharp):
    1. void Update(){
    2.         if (isHeld) {
    3.             gameObject.GetComponent<Rigidbody>().useGravity = false;
    4.             ActiveUse ();
    5.         } else {
    6.             countToThrow -= 1;
    7.             if(countToThrow == 0){
    8.                 this.gameObject.GetComponent<Rigidbody>().AddRelativeForce(0.0f,throwup,throwforward);
    9.                 CollisionsModel.enabled = true;
    10.                 gameObject.GetComponent<Rigidbody>().useGravity = true;
    11.             }
    12.         }
    13.     }
    The countdown timer is set to 2 when I call the DropWeapon Method

    Code (CSharp):
    1. public void DropWeapon(PlayerContentManager PCM, Gunscript Gs){
    2.         PCM.ActiveWeapon = null;
    3.         Gs.PCM = null;
    4.         Gs.isHeld = false;
    5.         Gs.countToThrow = 2;
    6.     }
    The countToThrow if statement definitely goes through 0 because the other lines in that if statement fire and I can observe it counting.
    However the force doesn't apply, I also am sure it has a rigid body the weight is the real weight of an AK47 and it only drops to the ground with gravity no matter what I set the throwforward or throwup variable to.
     
  2. daemon3000

    daemon3000

    Joined:
    Aug 13, 2012
    Posts:
    139
    I think you need to add the force as an impulse. The default force mode needs to be applied continuously.
    Use this instead:
    Code (CSharp):
    1. this.gameObject.GetComponent<Rigidbody>().AddRelativeForce(0.0f,throwup,throwforward, ForceMode.Impulse);
    And you should cache the rigidbody reference. It's bad for performance if you call GetComponent every frame.