Hey i want my object wich has multiple thrusters to be able to launch upwards or hover in place. For the Thrusters my code is:
parentRigidbody.AddForceAtPosition(transform.rotation * Vector2.up * force, transform.position);
parentRigidbody.AddForceAtPosition(transform.rotation * Vector2.up * floatforce, transform.position);
But i cant really calculate the floatforce needed.
The force is 23.42.
Just kidding. I don’t think you should try to calculate some force exactly to put in to achieve something that the system is supposed to calculate for you.
Hm… other words: I think if you need to calculate something the physic system is supposed to calculate, you are doing something wrong.
What you are looking for is a “negative feedback loop” to adjust your thrusters depending on either a target velocity or a target height. “target velocity” means the actual height of the craft does not matter to you - it should just hover mid-air, independent of how high it is above the ground). For this, the target y-value of the velocity would be 0. If you aim for a “target height”, that means you try to retain a specifc “height over the ground”. So the craft would adjust if its suddenly hovering over a hill.
I am now assuming you want to target for velocity.y = 0.
So in short: Read your current velocity and scale some max thrust by its negative value.
public float maxThrust = 42; // experiment with values to get some stable hovering
void FixedUpdate() // always do physic stuff in FixedUpdate.
// aim for zero vertical velocity
var ny = -parentRigidbody.velocity.y;
parentRigidbody.AddForceAtPosition(transform.rotation * Vector2.up * maxThrust * ny, transform.position);
Getting the “maxThrust” right depends on your mass and drag and some other stuff (like physics framerate). Basically try some values until the hover craft stops spinning or oscillating too much.
(Oh and my sample code assumes that the “thruster” can actually output negative thrust, i.e. they push in the opposite direction. If that is not the case for you, clamp the ny value at 0)
Some remark: If you just want to have something hover in air, why do you need any physics at all? Switch the rigidbody to kinematic and just set the positions you like to give the impression of hovering.