Laser Defender - Using Time.deltaTime

Hi,

In the block breaker section of the course when we moved the paddle left and right using our mouse we took the x value of the mouse position and normalized it with our screen width and assigned the new x value to the transform.position property of our paddle like so,

    // Update is called once per frame
    void Update()
    {
        //Normalize the current x position of the mouse w.r.t screen width
        //Convert it into unity world units by multiplying it with 16 (since 4:3 aspect ratio)

        Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);

        //Limit the paddle's movement to be within the min and max x range
        paddlePos.x = Mathf.Clamp(GetXPos(), minPaddleXValue, maxPaddleXValue);

        transform.position = paddlePos;
    }

     private float GetXPos()
    {
        //If Autoplay is enabled, then change paddle position to move along with ball position
        //Else move the paddle wherever the mouse moves
        if (gameStatus.IsAutoPlayEnabled())
        {
            return ball.transform.position.x;
        }
        else
        {
            return (Input.mousePosition.x / Screen.width) * screenWidthInUnits;
        }
    }

In this portion of code we are not using any Time.deltaTime for the movement of our paddle. So my question is does Time.deltaTime come into play only during keyboard / joystick movement? Is it not applicable for mouse movements ?

Sorry if this question is dumb :slight_smile: Thanks !

It really depends on the type of movement you are going after, but basically it depends on one thing: if the movement you are coding can be altered by frame rate (going slower or faster), then you have to use deltaTime or FixedUpdate, if not, then you can omit that, in this case, since the paddle is just “teleporting” to the mouse position you don’t need to, but for instance, if you want to follow the cursor instead of teleporting to it, then you’d have to use deltaTime.

It’s something a little hard to grasp at first but the more you code the easier it will get, it eventually becomes super obvious as to when use deltaTime.

Edit:

I forgot to mention that it doesn’t apply to movement only, but to other things, like firing rate, Fornite, for instance, if you play at 30 FPS some weapons fire slower, yeah… even pros have sometimes issues understanding this concepts.

1 Like

Thanks for that clarification :smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms