0
\$\begingroup\$

I am trying to learn how to move objects in Unity without using the built in features like AddForce etc. Some tutorials on Unity website (and other places) is where I have got most of my 'knowledge' so far from.

This script for moving players (in topdown Space SHMUP) has been working fine for me and includes acceleration and artificial drag (basically 'smooth' movement, or like being on ice).

I'm sure this code is overly long, bloated, inefficient and in most cases downright wrong, but it is written for me to understand it, rather than simply using Unity Prefabs and AssetPackages i download from Store.

QUESTION: Do I need the Time.fixedDeltaTime mutlipiers when calculating my 'delta_v' values here. And also what about on the drag part at the bottom.

As usual all help and comments are greatly appreciated. Thanks!

public class Player_Movement_SpaceShooter_TopDown : MonoBehaviour
{
public Vector2 maxVelocity;
public float drag;
public float moveForce;
private Rect moveBounds;

private Rigidbody2D rb;

private void Start()
{
    rb = GetComponent<Rigidbody2D>();
    moveBounds = Game_Manager.instance.ScreenBounds;
}

private void FixedUpdate()
{
    float input_h = Input.GetAxis("Horizontal");
    float input_v = Input.GetAxis("Vertical");
    Vector2 delta_v = Vector2.zero;

    delta_v.x += input_h * moveForce * Time.fixedDeltaTime;
    delta_v.x = Mathf.Clamp(delta_v.x, -maxVelocity.x, maxVelocity.x);
    delta_v.y += input_v * moveForce * Time.fixedDeltaTime;
    delta_v.y = Mathf.Clamp(delta_v.y, -maxVelocity.y, maxVelocity.y);

    Vector2 pos = rb.position;
    Vector2 vel = rb.velocity;
    if (pos.x < moveBounds.xMin)
    {
        pos.x = moveBounds.xMin;
        vel.x = 0f;
    }
    if (pos.x > moveBounds.xMax)
    {
        pos.x = moveBounds.xMax;
        vel.x = 0f;
    }
    if (pos.y < moveBounds.yMin)
    {
        pos.y = moveBounds.yMin;
        vel.y = 0f;
    }
    if (pos.y > moveBounds.yMax)
    {
        pos.y = moveBounds.yMax;
        vel.y = 0f;
    }

    rb.position = pos;


    rb.velocity = vel + delta_v;
    rb.velocity = rb.velocity * (1 - Time.fixedDeltaTime * drag);

}


}

It appears to me that if FixedUpdate was somehow slowed down, the velocity would get changed slower, the same with the enemy movement but I don't know how that would effect the gameplay and if/how the slowing-down would occur

\$\endgroup\$
2
\$\begingroup\$

It is important to use a delta time (like Time.fixedDeltaTime) because it will make your movement be consistent. If fixedUpdate is slower, fixedDeltaTime is bigger so the increase in delta_v will be bigger.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ OP's code contains both correct and incorrect uses of Time.fixedDeltaTime - can you help clarify when it should vs should not be used? \$\endgroup\$
    – DMGregory
    Jul 9 '19 at 22:05
  • \$\begingroup\$ I think it is the 'drag' part that is wrong. But every time I really sit and think about it, I cannot decide whether it should be applied or not... Also the drag feels right in game when set to 0.05f, which makes me feel like it is correct. The 'moveForce' part adds some velocity each step, and so i feel it should be factored with delta-time .... again I am still very unsure even though I have read much on the subject over many years, yet I have never really ad to worry too much about it and so never really got to the nitty-gritty of when/when not to use it \$\endgroup\$ Jul 10 '19 at 12:18
  • \$\begingroup\$ Your drag should be: 0.5 * -rb.velocity^2 * drag. So that gives you rb.velocity = 0.5 * -rb.velocity^2 * drag. No need for delta time as your drag is just a coefficient and rb.velocity is already affected by deltaT \$\endgroup\$
    – Ben
    Jul 10 '19 at 20:55
0
\$\begingroup\$

You use Time.FixedDeltaTime when writing MonoBehaviours that you expect to use across multiple project or to share with others, that may use a different FixedDeltaTime and you want a consistent speed based on real time.

Personally, for most cases, I would just add a comment that speed is based on game updates. In fact, that what I like to do.

Example:

public class PlayerSpriteBehaviour: MonoBehaviour {
    public float speed = 1.0f; // In pixels per frame
}

Then if I use 30 updates per second in a project and 60 in another, I have to adjust speed in my PlayerSpriteBehaviour. But I'm happy with it.

Note that to have the following formula:

position.x += speed * Time.FixedDeltaTime;

I would have to use a different comment here:

public class PlayerSpriteBehaviour: MonoBehaviour {
    public float speed = 60.0f; // In pixels per second
}

Answer: You can use it or not as long as you understand the implications.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.