Search Unity

  1. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

I cant addForce to rigidbody.

Discussion in 'Scripting' started by kardok, May 31, 2020.

  1. kardok

    kardok

    Joined:
    Mar 11, 2018
    Posts:
    17
    Dear Unity users,

    Here is my code:

    public bool bePushed;
    Rigidbody rb;

    void start(){
    rb= GetComponent<Rigidbody>();
    }

    void Update(){
    if (bePushed)
    {
    rb.AddForce(-transform.forward * 100);
    bePushed= false;
    }
    }

    So there is a giant enemy on my game. This code is at my Player. When Giant Enemy hits, bePushed is becoming true. I checked that part is working. And in if, bePushed=false is working to. But my character is not moving. How can i fix that? Please help me.

    Sincerely,
    Kardok.
     
    Last edited: May 31, 2020
  2. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    148
    Hi,
    you have some syntax errors:
    It is bool not Bool, Start() not start
    rb = GetComponent<Rigidbody>(); not with ==

    After that, use AddForce into FixedUpdate() not Update.
    And i think the player wont stop when bePushed = false... When you add a force, its continuous. If you want the player stops you have to slowdown/stop its velocity...
     
    kardok likes this.
  3. kardok

    kardok

    Joined:
    Mar 11, 2018
    Posts:
    17
    I fixed those part like u said. And added:

    void FixedUpdate()
    {
    if (bePushed)
    {
    rb.AddForce(-transform.forward* 100);
    bePushed=false;
    }
    }
    But it didnt work. Could it be because of something on rigidbody?
     
  4. kardok

    kardok

    Joined:
    Mar 11, 2018
    Posts:
    17
    Okay my rigidbody has isKinematic. So i will close it, push it and turn it on again. İt will probably work.
     
unityunity