Quick question about changing rigidbody behaviour

Hello,

I want to make an object float in 1 state, (with still an active rigidbody so it wont go trough walls) and have actual working gravity in another state. what would be an efficition way to do this? Please don’t give full scripts since I won’t learn from that, just give a few tips and I’ll be happy :smile:

With kind regards,
Frisout

Well here’s something i just thought up : lets have 2 states: state F (for float) and state D (for down) , so basically , you will just make the 2 trigger colliders(if you don’t know what a trigger collider is then it’s just a collider that you can go thru but still can detect stuff), and place them in State F and State D , and then add a script on each of them, for the State F one, just say when the object collides with the object (but can still go thru) , then disable the gravity for the object, so it can float. Similarly, for the State D collider, just make it the same but enable gravity instead of disable.
Sorry for making you read this essay :stuck_out_tongue: , I just wrote what I thought while making this, if you have any questions, or want me to provide a script (i will add messages explain what each line does), just tell me :smile:

1 Like

I already know when and how I will change the state, all I want is to know HOW to disable the gravity. Is there a certain thing like Gravity.Enabled (true) or whatever.

Thanks anyways for the reply :smile:

*Edit Too confused with all the 2D-threads at the moment. This does not work for the 3D version of the Rigidbody. My bad!

Try to set the gravityScale to 0 (no longer affected by gravity) or another scaling value if necessary.

1 Like

Is this on 1 object or the whole project?

You can use what Suddoha said,
but there is literary the exact thing you are searching for :

gameObject.GetComponent<Rigidbody> ().useGravity = false;
2 Likes

Alright, I’ll try in an hour, thanks so far :smile:

It is actually the thing he’s looking for, unless he’s doing 2D (then it would be gravity scale).
I got confused with all the 2D-threads I’ve been repsonding to recently, so I had Rigidbody2D in mind. :face_with_spiral_eyes:
:smile:

1 Like

It worked :smile:
thanks for the help all

I believe you want to add an equal and opposite force at the center of gravity of the object. The force required would be the opposite of the force that gravity applies on the mass. If that isn’t clear enough I can point you to the specifics.

ehm, this question just got solved already… and this reply didnt make any sense to me tbh but whatever, still thanks for trying to contribute :slight_smile:

I understand you found a solution. However your object is not “floating”, it simply does not have gravity applied to it. Here is how to actually make it float.

_rigidbody.AddForceAtPosition(Physics.gravity * _rigidbody.mass * -1f, _rigidbody.centerOfMass, ForceMode.Force);

It’s fine really, it is floating since you take temporal controll over the obect.
You know what just see for yourself.
https://forum.unity3d.com/threads/need-help-with-camera-going-out-of-bounds.493300/
In this new topic I made you can see a video where it works already