Search Unity

I cant addForce to rigidbody.

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

  1. kardok

    kardok

    Joined:
    Mar 11, 2018
    Posts:
    46
    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:
    257
    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:
    46
    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:
    46
    Okay my rigidbody has isKinematic. So i will close it, push it and turn it on again. İt will probably work.