0
\$\begingroup\$

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()
    {
        rigidBodyToMove.AddForce(force);
    }

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.

\$\endgroup\$
5
  • \$\begingroup\$ 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? \$\endgroup\$
    – Philipp
    Jul 6, 2021 at 12:12
  • 1
    \$\begingroup\$ Does this answer your question? How to move the player while also allowing forces to affect it? I also elaborate on how to retain vertical acceleration from gravity while exerting control horizontally in this answer. \$\endgroup\$
    – DMGregory
    Jul 6, 2021 at 13:27
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$
    – DMGregory
    Jul 7, 2021 at 17:24
  • \$\begingroup\$ @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. :) \$\endgroup\$
    – qqqqqqq
    Jul 8, 2021 at 8:20
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Jul 8, 2021 at 10:54

1 Answer 1

-1
\$\begingroup\$

It seems like you want your player input to enact a constant position change (MovePosition) on the character while having the velocity still be affected by the forces (AddForce) of wind, gravity, and other things in the physics engine.

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.

In regards to your concerns about "putting all the physics features not into the physics engine", this approach wouldn't interfere with any other forces like setting the velocity would, so gravity, wind, and collisions should work as normal (see above). This is the normal way of moving an object a certain distance independent of velocity, and you can actually see an example on the official Unity documentation for MovePosition.

\$\endgroup\$
7
  • \$\begingroup\$ 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. \$\endgroup\$
    – qqqqqqq
    Jul 8, 2021 at 8:22
  • \$\begingroup\$ That shouldn't be the case. Like I said, MovePosition doesn't affect velocity or acceleration at all. I've tested it myself and it has no effect on gravity or any other added force. \$\endgroup\$
    – David C.
    Jul 10, 2021 at 20:48
  • \$\begingroup\$ I think you may have a skewed view of what 'getting rid of the acceleration' is. Do you mean the object just stops in mid-air when you press the key? \$\endgroup\$
    – David C.
    Jul 10, 2021 at 20:50
  • \$\begingroup\$ Let us go simple. Do you know what an acceleration is? If you know, could you, please, right a definition of it, so that we would be on the same page. :) \$\endgroup\$
    – qqqqqqq
    Jul 11, 2021 at 7:04
  • \$\begingroup\$ Does the object stop in mid-air when you press the key? If you're unsure how MovePosition is affecting the acceleration and velocity, try checking the Rigidbody's stats in the Unity inspector. You should see only acceleration and velocity change with external forces and not with the input. MovePosition only changes the position while adhering to the other aspects of the physics engine like collisions - again, without affecting velocity or acceleration at all. \$\endgroup\$
    – David C.
    Jul 13, 2021 at 3:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .