Search Unity

Is there a way I can addforce to a ridgidbody without changinf its direction?

Discussion in 'Scripting' started by artonator, Jan 22, 2013.

  1. artonator

    artonator

    Joined:
    Sep 12, 2009
    Posts:
    69
    Hi, I would like to push a rigidbody when the rigidbody stays in an area to certain direction,

    When I tried addForce It actually gradually rotate it to the direction of the force so it end up looks as plane number 3 when I actually want is plane number 2.

    is there a way I can solve this with addForce without using "MovePosition"?





    Thank you,

    Arthur.

    PS incase you can`t see the image, you can take a look at this here.

    https://www.dropbox.com/s/91wd2fu0svqq665/Force.jpg
     
  2. Palani

    Palani

    Joined:
    Jan 17, 2013
    Posts:
    2
    try this code

    bool changeDir

    if(changeDir){
    changeDir=false;
    ball.rigidbody.AddForce (changeDir, 0, 0);
    }
     
  3. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    You went all out for the pic, lol. One thing to try is to check the freeze rotation box on the rigidbody.

    It can be done from code also, if you want to turn it on and off when needed.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    wouldn't 3 occur when you continually apply a force in that direction over and over? are you doing this in the update function?
     
  5. artonator

    artonator

    Joined:
    Sep 12, 2009
    Posts:
    69
    Thank you guys your replies,

    @ LeftyRighty - I put it in "onCollosionStay" method, because I need to apply force when the object is in that "area"

    @Black Mantis - I freeze all the rotations already.

    @ Palani - I can`t see how applying zero vector to a vector will do anything...
     
  6. Palani

    Palani

    Joined:
    Jan 17, 2013
    Posts:
    2
    //Ball object is already moving certain direction
    ball.rigidbody.AddForce(10,10,10);

    //here added extra force on condition
    if(changeDir){
    changeDir=false;
    ball.rigidbody.AddForce (changeDir, 0, 0);
    }
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    onCollisionstay gets called once per frame so rather than a one off "bump" to knock it off course to position 2, it's a constant push which ends up in position 3.

    If you're modelling a crosswind during flight (from the pic) wouldn't the plane need to steer into the wind in order to continue straight? i.e. it's forward motion force needs to shift to counter the the cross wind component.
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Did you even bother to test this before posting it.....twice?

    LeftyRighty is correct in both counts it would seem. The plane needs to compensate for the force in order to stay on course and you shouldn't apply the force every frame.