Search Unity

Need help with Rigidbody.AddForceAtPosition

Discussion in 'Physics' started by unity_OHgIojcN8QZHsg, Nov 5, 2019.

  1. unity_OHgIojcN8QZHsg

    unity_OHgIojcN8QZHsg

    Joined:
    Nov 5, 2019
    Posts:
    1
    I'm trying to make a create-your-own-plane and flight simulator game that will allow users to place engines at various positions around the plane.

    Because of this I want to use Rigidbody.AddForceAtPosition to apply the thrust produced by each engine wherever it may be and at whatever orientation. I have set up a simple test with my plane with one propeller at the nose but I'm having an issue with it - the plane is pulled forward as expected but as soon as I try and pull up, the force from the engine is immediately slamming the nose back into the ground.

    The associated code is here (thrust is just a float I have hard-coded at this stage):

    Code (CSharp):
    1. private void UpdateThrust()
    2.        {
    3.            foreach (var engine in engines)
    4.            {
    5.                var thrust = engine.CalculateThrust(inputManager.Throttle,                    
    6.                         RigidBody.velocity.magnitude);
    7.                var direction = engine.transform.forward;
    8.                var position = engine.transform.position;
    9.  
    10.                RigidBody.AddForceAtPosition(thrust * direction, position);
    11.            }
    12.        }
    Have I made a mistake, or is this a side effect of using this method and can anyone suggest a solution?
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    Ensure that UpdateThrust is called from FixedUpdate only, not from Update. Everything else looks good to me from Unity physics side.

    As for simulating an airplane, also ensure to apply some kind of drag force behind the position of the engines. This should provide the aerodynamic stability.