[Question] different moving implementation - pros and cons

Hi guys, really really great course!

Instead of using your method, of moving the formation on every update, I implemented the moving of the formation in a little bit different way (removed unrelated code for brevity):

    void Start () {
        float distance = transform.position.z - Camera.main.transform.position.z;
        var leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distance));
        var rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distance));
        xmin = leftmost.x + (0.5f * width);
        xmax = rightmost.x - (0.5f * width);
        GetComponent<Rigidbody2D>().velocity = Vector3.right * speed;
    }

    void Update () {
        if (transform.position.x >= xmax)
        {
            GetComponent<Rigidbody2D>().velocity = Vector3.left * speed;
        }
        else if (transform.position.x <= xmin)
        {
            GetComponent<Rigidbody2D>().velocity = Vector3.right * speed;
        }
    }  

I got a few questions to ask:

  1. Is there any pros and cons of using either method?
  2. I don’t really see how can I use the Time.deltaTime. Do I even need to do it, or the velocity is automatically considering the frame rate?
  3. I notice that when I scale the speed up and down during play mode, the speed doesn’t always change. Sometimes it does. Sometimes it doesn’t. I’m working on windows. Is this a bug?
    (4. I notice that I have a small bug in my code. Let’s see who finds it first :smiley: )

Thx,

You should multiply your Vector3.left * speed and Vector3.right * speed by Time.deltaTime to ensure framerate independent calculation of its movement. This makes it so the ships always move at the same speed regardless of what type of device is using it. Example: A slow mobile phone versus a fast gaming PC should show identical movement speeds on their screens for the ships, even with drastically different hardware capabilities.