Search Unity

How to apply force at a specific position on a body

Discussion in '2D' started by indy2005, Dec 1, 2013.

  1. indy2005

    indy2005

    Joined:
    Aug 22, 2009
    Posts:
    81
    Hi,

    I have a space ship, and it needs to have a left, middle and right thruster. I was hoping to simply weld 3 thruster rigid bodies to the space ship and simply apply a force directly to the thrusters, but there is no box2D weld joint.

    So I think I need to add 3 child game objects which are not physics objects to the space ship, and apply a force to the main spaceship at these positions. Is this the correct approach, and how do I apply a force at those positions...do I apply a force at a world position, or a local position of the spaceship - if world space, how do I get the position vector i need to apply the force at.

    Any help appreciated.

    Regards

    Stephen
     
  2. adscott1982

    adscott1982

    Joined:
    Mar 16, 2012
    Posts:
    21
  3. indy2005

    indy2005

    Joined:
    Aug 22, 2009
    Posts:
    81
    Hi,

    Thanks, it was easier than I thought...transform gives world space anyway, even on child objects so no translation required.

    Got in a pickle for an hour, because I forgot to Mathf.Abs the horizontal access value and so it only ever turned one way ;-)


    public class LanderScript : MonoBehaviour {

    public GameObject leftThruster;
    public GameObject rightThruster;
    public GameObject mainThruster;
    public float thrustForce = 2f;
    public float sideThrustForce = 2f;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    Camera mainCamera = Camera.main;

    float leftRightThrust = Input.GetAxis("Horizontal");
    float mainThrust;


    if(Input.GetKey("joystick button 16")) {
    mainThrust = 1.0f;
    } else {
    mainThrust = 0f;
    }

    //Vector3 leftThrustPos = transform.TransformPoint(leftThruster.transform.position);
    Vector2 leftThrustPos = new Vector2(leftThruster.transform.position.x, leftThruster.transform.position.y);
    Vector2 rightThrustPos = new Vector2(rightThruster.transform.position.x, rightThruster.transform.position.y);
    Vector2 mainThrustPos = new Vector2(mainThruster.transform.position.x, mainThruster.transform.position.y);

    Debug.Log("leftThrustPos " + leftThrustPos.x + " " + leftThrustPos.y);
    Debug.Log("rightThrustPos " + rightThrustPos.x + " " + rightThrustPos.y);
    Debug.Log("mainThrustPos " + mainThrustPos.x + " " + mainThrustPos.y);

    if(leftRightThrust < -0.2) {
    Debug.Log("applying thrust to turn left");

    rigidbody2D.AddForceAtPosition(transform.up * Mathf.Abs(leftRightThrust) * sideThrustForce, rightThrustPos);
    } else if(leftRightThrust > 0.2) {
    Debug.Log("applying thrust to turn right");

    rigidbody2D.AddForceAtPosition(transform.up * Mathf.Abs(leftRightThrust) * sideThrustForce, leftThrustPos);
    }

    if(mainThrust > 0) {
    rigidbody2D.AddForceAtPosition(transform.up * thrustForce, mainThrustPos);
    }
    //leftThruster.AddForce(Vector2.up * thrust);
    }
    }