Here’s my scenario:
I’ve got a hovercraft with four “thrusters” that lift it off the ground. Each thruster uses AddForceAtPosition to push the hovercraft, and the player controls the craft by increasing or decreasing the amount of force that each thruster applies.
I want to be able to automatically make the hovercraft hover to a stop in mid-air. What I want to do is this:
- AddForceAtPosition for each thruster.
- Figure out the total force and torque being applied by the thrusters, plus the force of gravity.
- Slowly apply an opposite force to bring the hovercraft to a halt.
Step 2 is the part that I can’t figure out how to do with Unity.
My current work-around gets the velocity, and then applies an opposite force. This almost brings it to a stop, but not quite. When I calculate force from velocity, but the velocity doesn’t update immediately when you apply force. This code demonstrates issue is:
FixedUpdate() {
Debug.log(rigidB.velocity);
//output: (0, 0, 0)
rigidB.AddForceAtPosition(new Vector3(100, 0, 0), new Vector3(10, 0, 0));
Debug.log(rigidB.velocity);
//output: (0, 0, 0)
}
I wish I could do something like this:
FixedUpdate() {
rigidB.AddForce(new Vector3(100, 0, 0));
rigidB.AddForce(new Vector3(100, 0, 0));
Debug.log(rigidB.forces);
//output: (200, 0, 0)
}
But I don’t think there’s any way to access the current force being applied to a rigidbody.
If I was just using AddForce, I could do this
FixedUpdate() {
Debug.log(rigidB.velocity);
//output: (0, 0, 0)
Vector3 totalForce = Vector3.zero;
totalForce += new Vector3(100, 0, 0));
totalForce += new Vector3(100, 0, 0));
Debug.log(totalForce );
//output: (200, 0, 0)
}
But that won’t work when I use AddForceAtPosition. So what I’m asking for is a “ForceAtPosition to Force” that can be broken-down into force and torque. Something like this:
ForceAtPosition f = new ForceAtPosition(new Vector3(0, 10, 0), new Vector3(5, 0, 0), rigidB);
Debug.log(f.force);
//output: (3.2, 3.2, 0)
Debug.log(f.torque);
//output: (5, 0, 0)
So, does anyone know how ForceAtPosition is calculated and turned into a force and a torque?