- Home /
Simulate rotation using two force thrusters
I'm currently working on a 2D space shooter game. I've decided to model everything using pure Newtonian mechanics. Is there a way to simulate rotation using two force thrusters?
What I do at the moment is I create a parent GameObject and put two thruster objects into it some distance apart of each other. I then apply force to those thrusters in hopes of moving the parent object, sadly nothing happens. What am I doing wrong?
Answer by EvilTak · Dec 25, 2014 at 12:54 PM
You can use Rigidbody2D.AddForceAtPosition. You can use a simple script as follows:
public Transform leftThruster;
public Transform rightThruster;
public float force;
void FixedUpdate(){
Vector2 forward = new Vector2(transform.up.x, transform.up.y);
//Thrusters
Vector2 leftThrusterPos = new Vector2(leftThruster.position.x, leftThruster.position.y);
rigidbody2d.AddForceAtPosition(force * forward, leftThrusterPos);
//Same for rightThruster
}
You can modify the code to add force only to one thruster to turn, i.e. when the left key is pressed, the right thruster will only provide the thrust and vice versa.
Thanks, it took me a while to figure it out for myself, but I finally did it using the method you've described (a more encapsulated version with a thruster object). :) I've checked back to add this as an answer, but you've already beaten me to it.
The parent will move the children relative to it, but the inverse of it is false. Would be nice if it could've been a built-in feature to enable such an option though.
Before I thought a joint might be a solution (sadly fixedJoined2D does not exist), but all I had to do was to read the API reference more closely.