EDIT: Reading the answer DMGregory linked I know understand why setting the velocity directly is not advisable, but now I have some new questions:
Is it generally advisable to avoid instant force modes like
Impulse
andVelocity
when it comes to things like character movement? I'm assumingVelocityChange
functions nearly exactly like setting velocity directly. Am I right?I've started testing movement using the example code in DMGregory's answer of making a body accelerate to a target velocity
private void FixedUpdate() { Vector2 input = GetInput(); if (Mathf.Abs(input.x) > float.Epsilon || Mathf.Abs(input.y) > float.Epsilon) { Vector3 desiredVelocity = new Vector3(input.x * speed, 0f, input.y * speed); Vector3 delta = desiredVelocity - body.velocity; Vector3 acceleration = delta / Time.deltaTime; if (acceleration.sqrMagnitude > maxAccel * maxAccel) { acceleration = acceleration.normalized * maxAccel; } body.AddForce(acceleration, ForceMode.Acceleration); } }
and I've encountered the following curious behavior: https://imgur.com/snGyVYK When moving the body by applying force, the body seems to levitate as if gravity is not being applied. It actually is being applied but it seems as if the force being applied to the body is overriding the force of gravity because as soon as I let go of the movement keys the body suddenly drops. I am not applying any force on the y axis so I'm not sure what exactly is at play here. What is going on here and what can I do to mitigate this behavior?