- Home /
Question by
robert_val1 · Apr 12, 2018 at 01:26 PM ·
performancevelocityjumpingif-statementsframerate drops
Drop in performance when checking velocity of rigidbody.,Performance drop when checking velocity of rigidbody.
if (GetComponent().velocity.y == 0) { transform.Translate(Input.GetAxis("Horizontal") Time.deltaTime speed, 0, Input.GetAxis("Vertical") Time.deltaTime speed); }
// When I use this in my jump script it makes my framerate inconsistent and jittery.
Comment
Answer by sean244 · Apr 14, 2018 at 01:56 AM
You keep calling GetComponent and Input.GetAxis inside the update. You need to cache these. Also, since it's a physics operation, it should be in FixedUpdate.
private Rigidbody2D _rb;
private float speed;
private void Start ()
{
_rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate ()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
if (_rb.velocity.y == 0f)
{
transform.Translate(horizontal * Time.fixedDeltaTime * speed, vertical * Time.fixedDeltaTime * speed, 0);
}
}