Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Rigidbody.AddForce not working?

Discussion in 'Scripting' started by norman251, Dec 7, 2016.

  1. norman251

    norman251

    Joined:
    Dec 7, 2016
    Posts:
    5
    Hello!

    I am currently trying to implement jump pads into my game. I have a trigger that detects when the player is on the jump pad and when the player is pressing space. If both of those are true, it uses AddForce to boost the player upwards. However, when I jump on the pad, it doesn't boost me, even though my Debug.Logs are triggering.

    The dots represent irrelevant code.

    Here are the lines of code:
    Code (CSharp):
    1. ...
    2. private Rigidbody playerBody;
    3. public int boostHeight;
    4. ...
    5. void Start () {
    6.  
    7. ...
    8. player = GameObect.FindGameObjectWithTag("Player");
    9. ...
    10. playerBody = player.GetComponent<Rigidbody>();
    11.  
    12. }
    13. ...
    14. if (state == State.boosting) {
    15.  
    16. Debug.Log("Boosting")
    17.  
    18. playerBody.AddForce(Vector3.up * boostHeigh, ForceMode.VelocityChange);
    19.  
    20. }
    The "state" is an enum I set the value of in OnTriggerEnter, and it works just fine. The boostHeight is set in the editor to 200, and the if (state == State.boosting) is getting triggered, but for some reason the AddForce function just isn't working, and, being a complete NOOB, I have no idea why.

    Please help me,
    -N251

    P.S. Yes, the player has a Rigidbody. However, I am using the default FirstPersonController, and I have heard that using it might cause some problems.
     
  2. norman251

    norman251

    Joined:
    Dec 7, 2016
    Posts:
    5
    Also, if there is an easier way to create jump pads, PLEASE let me know.

    Thanks again!
    -N251
     
  3. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    I am assuming that the Debug.Log is being called, and the playerBody is not equal to null (this would throw an exception in the console). I'm also assuming that "boostHeigh" is a typo only in your post, and not in your actual code (as this would not compile!). Try writing this code in place of your AddForce:

    Code (csharp):
    1. playerBody.velocity = Vector3.up* boostHeight;
    This won't be the exact effect you're going for, but it will let you know that you're able to properly communicate with the rigidbody. Make sure that boostHeight is set to a reasonably high value to start as well, to make sure you're not just adding an extremely tiny force (although ForceMode.VelocityChange adds a force independent of the rigidbody's mass).
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    The default FirstPersonController is almost certainly resetting your forces the very next frame -- assuming everything else works.
     
  5. norman251

    norman251

    Joined:
    Dec 7, 2016
    Posts:
    5
    It didn't work, but I don't think it's because I can't communicate with the Rigidbody. It might be as GroZZleR said,

    Does that mean that I would have to make my own FirstPersonController?

    Thanks for the help!
    -N251
     
  6. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    To me it looks like it isn't working because you have the code within the "Start" method. On your booster object (the item that you stand on) I would add a trigger to it, then run the code in the trigger like this (untested):

    Code (CSharp):
    1. public class Booster : MonoBehaviour {
    2.  
    3.     public float boostHeight = 0;
    4.  
    5.     public void OnTriggerEnter(Collider other) {
    6.         Rigidbody playerBody = other.GetComponent<Rigidbody>();
    7.         if (other.gameObject.tag == "Player" && playerBody) {
    8.             var state = other.gameObject<Player>().state;
    9.             if (state == State.boosting) {
    10.                 playerBody.AddForce(Vector3.up * boostHeight, ForceMode.VelocityChange);
    11.                 state = State.notBoosting;
    12.             }
    13.         }
    14.     }
    15.  
    16. }
     
  7. norman251

    norman251

    Joined:
    Dec 7, 2016
    Posts:
    5
    I'm really sorry, I forgot to format and include things, and other stuff. Let me just post the entire document here. Of course, you will have to have Visual Studio to open it, or some other program, but I'm assuming you already have that.

    This script is attached to a trigger that is above a prism that is on the ground.

    Hope this helps.

    -N251
     
    Last edited: Dec 9, 2016
  8. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    The link is broken.
     
  9. norman251

    norman251

    Joined:
    Dec 7, 2016
    Posts:
    5
    It has been fixed. I hope that you guys are able to deduce what I am doing wrong.
     
  10. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You're going to have to reconcile your script with your character controller, as the default first person controller uses CharacterController.Move to explicitly manipulate the transform of the object.
     
  11. d4rk_1nf1n1ty

    d4rk_1nf1n1ty

    Joined:
    Jan 2, 2021
    Posts:
    2
    Hey, I realize this is an old post, but for anyone else here with a similar issue, like me: one thing that helped me was changing the Mass of my character. Setting it back to the default = 1 seemed to get it working, but more "realistic" values (representing kg) seemed to give "unresponsive" behavior, even though the code was working. Hope this helps someone!
     
    ChillenHippie likes this.