Hey all!
Wondering really quickly if there’s a good C# way of allowing AddForce to apply decimal forces to RigidBodies.
I’m making a little blimp simulator to accustom myself to Unity, and I’d love to get it so that I can have acceleration and turning forces, as well as natural buoyancy (i.e. upward lift to counteract gravitational pull, which would allow me to code altitude adjustments by increasing/decreasing the blimp’s innate lift). The acceleration and turning acceleration would likewise increase as the throttle increases/ship turns more, up to a certain maximum speed.
However these all seem like they would function best with decimal values, unless it would be more convenient to simply change the global gravity property to be, say, 981 instead of 9.81 and scale everything accordingly. Thoughts? Anyone have any experience creating this sort of “anti-gravity” lift force, or this sort of forward angular acceleration?
Finally…I can’t seem to find any good tutorials for creating a game scale. If, for example, I want to be able to measure size of my map (and thus my movement speeds) in meters, is there an easy way to lock down a grid scale to scale my terrain appropriately?
Thanks in advance.
-The Djinn
I haven’t worked on any projects like these but I can give you some tips.
Use FixedUpdate for updating forces or any physics related stuff.
You can indeed change the gravity to 981 instead of 9.81 from Edit->Project Settings → Physics or you can keep it at 9.81 and change the sleep velocity and sleep angular velocity lower if needed.
You can decrease the mass of the blimp (Rigidbody) and use AddForce and provide ForceMode.Force instead of acceleration to get reduce the force effect.
You can play with Drag and AngularDrag of the Rigidbody to make it more natural and get fixed velocity after reaching a certain speed.
Thanks! That’s fairly helpful.
I notice you didn’t address the decimal values though (or, if you did, I missed it). If I wanted to counter gravity by applying a 9.81m/s inherent upward force to the object, what would you suggest as the best way to do that? Given that I can’t seem to make AddForce counteract the natural gravity.
This could, of course, be mostly due to my non-knowledge of Unity at the moment.
This will counter gravity
void FixedUpdate()
{
var body = this.GetComponent<Rigidbody>();
var normal = body.mass * Physics.gravity * -1;
body.AddForce(normal, ForceMode.Force);
}
Not sure if I understand what you mean, because 1 unit is supposed to be 1 m…
That’s also why the gravity is 9.81.
Everything in the physics is adjusted to work in the scale of 1 unit = 1 m and it you go way beyond that scale in any direction, be prepared to see growing imprecisions. The mass on rigidBodies is also in kg.
You can pretty much calculate with real world values, use normal physic formulas for lift of certain gases etc.
Here’s a script which I used to simulate buoyancy of a Basketball in Water:
public float buoyancyForce = 70f;
public float waterY = 0;
public float waterDrag = 0.07f;
void FixedUpdate() {
Vector3 pos = transform.position;
Bounds bounds = transform.collider.bounds;
if (bounds.min.y < waterY) {
float submerged = Mathf.Min(1f,(waterY-bounds.min.y)/bounds.size.y); // submerged percentage
float submergedVolume = 0.5f*(1-Mathf.Cos(submerged*Mathf.PI));
float force = submergedVolume * buoyancyForce;
rigidbody.AddForceAtPosition(Vector3.up*force, pos);
rigidbody.velocity *= (1-waterDrag);
rigidbody.angularVelocity *= (1-waterDrag*0.2f);
}
}
I looked up the size and weight of a baskettball in wikipedia and set the mass of the rigidbody right.
I then ignore the mass of the rigidbody in the script, because I calculated the lift from the size and weight of the basketbal to be around 70 Newton and put that constant in the script.
The buoyancy calculation is physically correct and the ball floats as you would expect it, dipping about 25% - 30% into the water.
Only the valocity and angularVelocity were eyeballed by what looks good and of course I don’t calculate stuff like surface tension that sucks the ball a bit in when it tries to jump out of the water, but the behaviour looked good enough for a game and multiple balls were also pushing each other around in a realistic way etc.
The formula in the submergedVolume variable is for sphere-shaped objects and takes into account that if you dip a ball the first 25% into the water, you don’t displace 25% of the volume etc.