# How could I move an object, while respecting the gravity and other related physics in Unity?

How could I move an object, while respecting the gravity and other related physics in Unity?

I tried using the following approach:

    void FixedUpdate()
{
rigidBodyToMove.velocity = velocity;
}


The problem with that approach is, for example, when the object falls from a height there is no acceleration of a fall. Also, I am sure that there are other physics related things which do not get respected with that approach.

So, I tried using this:

    void FixedUpdate()
{
}


Now the problem with this approach is that, for example, when moving horizontally the object gets acceleration, while I want to just move it.

So, my question is basically how could I move an object (in my case a character, using an input from user), while respecting all the physics. I kind of want to have various physical forces which act on the object (gravity, wind, collisions...) but then I want my movement input to not act like a physical force but instead act like a constant velocity layered on top of all the physical movement.

UPDATE

There are four ways I know how to change the velocity (neither of them suits me, hence the reason for this question being posted):

1 Change velocity each frame by setting it:

rigidBodyToMove.velocity = velocity;


It is not appropriate for me, because this way velocity stays constant and ignores physics accelerations (e.g. gravity acceleration during a fall).

2 Change velocity each frame by incrementing it:

rigidBodyToMove.velocity += velocity;


This approach does not suit me either, because now I will add acceleration. Between the first increment and the second increment the rigidBodyToMove.velocity decreases and for sure needs to be incremented by some value to remain a constant movement force, but it may decrease or increase by a greater absolute value than the value of the velocity.

3 Change the velocity only once. This does not work as well, because the next frame Unity will recompute velocity and I will lose my applied movement force. And also I am losing the physcis forces with this approach.

4 Increment the velocity only once. Similar to 3, Unity will recompute the velocity and I will lose my movement force the next frame.

UPDATE 2

I tried to use the following approach : rigidBodyToMove.MovePosition(rigidBodyToMove.position + velocity*Time.deltaTime); The effect was exactly the same as in the 1 above. I.e. e.g. during a fall if I will start moving the fall acceleration disappears.

• I think your actual problem could be that you are having various physical forces which act on your object (gravity, wind, collisions...) but then want your movement to not act like a physical force but instead act like a constant velocity layered on top of all the physical movement. Does that sound right? Jul 6, 2021 at 12:12
• Comments are not for extended discussion; this conversation has been moved to chat. Jul 7, 2021 at 17:24
• @DMGregory, just curious why did not you move your question to the chat? It makes it look as if I did not explain why the linked questions do not work for me. Also, since Philip updated the question respectively his comment is not needed anymore. :) Jul 8, 2021 at 8:20
• I thought the links might be useful to other readers who find this thread. I also found Philipp's comment to be a concise summary of the goals — moreso than the question itself. The full conversation history is preserved in the linked chat thread. Jul 8, 2021 at 10:54

The player velocity, being a separate type of movement, can simply be hardcoded using MovePosition(position + direction * Time.deltaTime). This function will not affect the existing velocity and acceleration and takes into account collisions in the same way that it normally would, although low framerates might cause clipping issues in certain circumstances if you're not using the right interpolation mode. Also, make sure that direction is only the direction given by the input (i.e. left or right) and not the 'velocity' of the rigid body.
• That does not work for me. Imagine an object falling, the object gathers some acceleration. And now I press a button to move right while falling, then the MovePosition will just get rid from the acceleration. Jul 8, 2021 at 8:22