Performance: Input axis for smooth movement
This concept affects the frame rendering smoothness and input responsiveness during player movement.
float horizontal = Input.GetAxis("Horizontal"); transform.Translate(horizontal * speed * Time.deltaTime, 0, 0);
float horizontal = Input.GetKey(KeyCode.LeftArrow) ? -1 : Input.GetKey(KeyCode.RightArrow) ? 1 : 0; transform.Translate(horizontal * speed * Time.deltaTime, 0, 0);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Raw key input with immediate translation | N/A (game objects) | High frequency position updates | High due to jitter | [X] Bad |
| Input.GetAxis with smoothing | N/A (game objects) | Smooth position updates | Lower paint cost due to smooth movement | [OK] Good |
Input.GetAxis function return when used for movement?Input.GetAxis returns a float value that smoothly changes between -1 and 1 based on input device movement, like joystick or keyboard keys.Input.GetButton or Input.GetKey, it does not return boolean or integer but a float for smooth control.Input.GetAxis requires a string parameter with axis name in quotes, e.g., "Horizontal".float horizontal = Input.GetAxis("Horizontal");
float speed = 5f;
float movement = horizontal * speed * Time.deltaTime;
Debug.Log(Math.Round(movement, 2));float move = Input.GetAxis(Horizontal); transform.Translate(move * speed * Time.deltaTime, 0, 0);
float h = Input.GetAxis("Horizontal");
if (h > 0.1f) {
transform.Translate(h * speed * Time.deltaTime, 0, 0);
}
B) float h = Input.GetAxis("Horizontal");
if (Mathf.Abs(h) > 0.1f) {
transform.Translate(h * speed * Time.deltaTime, 0, 0);
}
C) float h = Input.GetAxis("Horizontal");
if (h > 0.1f && h < -0.1f) {
transform.Translate(h * speed * Time.deltaTime, 0, 0);
}
D) float h = Input.GetAxis("Horizontal");
if (h != 0) {
transform.Translate(h * speed * Time.deltaTime, 0, 0);
}Pass the test and this pattern moves unknown