- Home /
How to levitate a rigidbody and still use gravity on him at any time
Hi everyone,
I'm learning Unity and I have a question about the right way to do something. I have a scene with a sphere (my player) and a rigidbody attached, I can move it. For movements I was using MovePosition and just found its a bad way to do it so now I use forces. I would like the same kind of indication for another thing.
I want my player to levitate above ground at a fix distance. However, I want to apply a force like gravity to him. And I would like that if I make him fly then fall / fall of cliffs using that gravity force, he fall but stop before ground, where he is supposed to levitate.
I really don't know how to approach this. I read about Raycast below player to check distance, but maybe there is some better ways to do it that I don't know? Maybe this is a terrible way like the MovePosition thing?
Any help appreciated, sorry if it's a naive question ^^"
EDIT : can't success to do line breaks with the indications......
Do you want the floating to have a bobbing, dynamic effect, or just a constant distance all the time? In the second case you could move the visual model (e.g. $$anonymous$$eshFilter and $$anonymous$$eshRenderer for 3D) into a child game object and offset it by the desired distance in y.
(Btw, for line breaks you need to use the html tag for line breaks, <br>
)
Answer by FlaSh-G · May 28, 2018 at 01:15 PM
Apply a force with ForceMode.VelocityChange in the opposite direction of your gravity in FixedUpdate.
private Rigidbody rb;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
rb.AddForce(-Physics.gravity, ForceMode.VelocityChange);
}
Your answer
Follow this Question
Related Questions
Move rigidbody to position and still have forces act on it 0 Answers
Physics update rate problems 1 Answer
Apply force to rigidbody based on child object acceleration causes jitter 1 Answer
How do I get force applied on an object by unity physics? 0 Answers
How can I get a responsive rigidbody FPS controller without acceleration that reacts to forces? 2 Answers