- Home /
In the breakout tutorial,why does the paddle movement use Update() instead of FixedUpdate()?
I'm watching the Breakout tutorial, and I would like to know how this code works. Here's the code:
public class Paddle : MonoBehaviour {
public float paddleSpeed = 1f;
private Vector3 playerPos = new Vector3 (0, -9.5f, 0);
void Update ()
{
float xPos = transform.position.x + (Input.GetAxis("Horizontal") * paddleSpeed);
playerPos = new Vector3 (Mathf.Clamp (xPos, -8f, 8f), -9.5f, 0f);
transform.position = playerPos;
}
}
Wouldn't that way of moving the paddle cause it to be dependent on the framerate? So, if I had a higher framerate the paddle would move faster? I'd like to know if this is a good way to move the paddle, why/why not and in that case which would be a better way. Thanks.
Answer by Hellium · Feb 12, 2018 at 11:05 AM
The code inside FixedUpdate
will run according to the Physics engine. It should run at a approximate constant frequency.
The code inside Update
will run before a frame is rendered. As indicated in the documentation Input.GetAxis
is frame-rate independent.
However, because the paddle has a Rigidbody used to detect collision with the ball, I don't understand why FixedUpdate
+ rigidbody.MovePosition
is not used here.
Oh, I din't know that about GetAxis! Although I still don't understand how GetAxis can be frame independent as it only returns a float between -1 and 1, and the you use it for whatever you want. Anyway, thank you!
Also in the documentation they multiply by DeltaTime but not in the tutorial?