Search Unity

How is AddForceAtPosition() _supposed_ to work?

Discussion in 'Scripting' started by Jim Offerman, Aug 24, 2009.

  1. Jim Offerman

    Jim Offerman

    Joined:
    Jul 17, 2009
    Posts:
    177
    My aim was to simulate an object being propelled by a thruster, so I wrote:

    Code (csharp):
    1.  
    2. var m_thrusterPos : Vector3(0, 0, -1);   // local-space thruster position
    3. var m_thrusterForce : Vector3(0, 0, 500);   // thruster force in local-space  
    4.  
    5. function FixedUpdate()
    6. {
    7.    var wPos : Vector3 = transform.TransformPoint(m_thrusterPos);
    8.    var wForce : Vector3 = transform.TransformDirection(m_thrusterForce);
    9.    rigidbody.AddForceAtPosition(wPos, wForce);
    10. }
    11.  
    On pressing run, I expected my object to start moving under the force of the thruster. Instead, it just sorta kinda sat there. Not moving. At all. :(

    After trying a bunch of different things, I got it to move the way I expected by doing this instead:

    Code (csharp):
    1.  
    2. function FixedUpdate()
    3. {
    4.    rigidbody.AddRelativeForce(m_thrusterForce);
    5.    rigidbody.AddRelativeTorque(Vector3.Cross(m_thrusterPos, m_thrusterForce));
    6. }
    7.  
    I'd expect this to be equivalent to the version I wrote above, except it's not. So... what am I missing here? How is AddForceAtPosition supposed to work?
     
  2. cyb3rmaniak

    cyb3rmaniak

    Joined:
    Dec 10, 2007
    Posts:
    162
    Well, you switched the order of the parameters...
    Before I'm gonna try and break my brain at 3am on the rest of the code - try and put them in the correct order and post the results :)
     
  3. Jim Offerman

    Jim Offerman

    Joined:
    Jul 17, 2009
    Posts:
    177
    iSuck indeed! ;)

    Thanks for spotting the blatantly obvious error my brain failed to register! Changing the order of the parameters, things work as advertised now. Yay!

    I think I'll keep the implementation that uses RelativeForce and RelativeTorque, though.