0
\$\begingroup\$

My game isn't smooth, it runs at 60 fps but has quick lag spikes just under every second. I really can't figure out why. I have a feeling it's to do with my player movement code. My rigidbody's interpolation is set to interpolate, and collision detection to continuous. Here is a sample of what it looks like. https://gfycat.com/cleanmeagerhuemul

public ParticleSystem particleLauncher;

public float playerSpeed;
public float jumpSpeed;
public float coolDownTime = 2;
public CameraShake cameraShake;
public bool dashEnabled;
public bool isCamShakeEnabled;

private bool isJumping;
private float move;
private Rigidbody2D rb;
private bool facingRight;
private float nextDashTime = 0;

public bool movementAllowed;

private Animator anim;

// Start is called before the first frame update
void Start()
{
    isCamShakeEnabled = true;
    Scene currentScene = SceneManager.GetActiveScene();
    string sceneName = currentScene.name;
    dashEnabled = true;
    anim = GetComponent<Animator>();
    facingRight = true;
    rb = GetComponent<Rigidbody2D>();
}


void FixedUpdate()
{
    if(movementAllowed == true)
    if (Input.GetButtonDown("Jump") && !isJumping)
    {
        rb.AddForce(new Vector2(rb.velocity.x, jumpSpeed));
        isJumping = true;
    }
}
// Update is called once per frame
void Update()
{
    Application.targetFrameRate = 60;
    if (movementAllowed == true)
    {
        move = Input.GetAxisRaw("Horizontal");

        rb.velocity = new Vector2(move * playerSpeed, rb.velocity.y);


        if (isJumping == true)
        {
            anim.SetBool("isJumping", true);
        }
        if(isJumping == false)
        {
            anim.SetBool("isJumping", false);
        }


        Flip(move);

        if (move == 0)
        {
            anim.SetBool("isRunning", false);
        }
        else
        {
            anim.SetBool("isRunning", true);
        }

        if (Time.time > nextDashTime)
        {
            if (Input.GetButtonDown("Dash") && dashEnabled == true)
            {
                if(SceneManager.GetActiveScene() == SceneManager.GetSceneByName("Level4-2"))
                {
                    nextDashTime = Time.time + coolDownTime;
                    StartCoroutine("DashMove");
                }
                else
                {
                    nextDashTime = Time.time + coolDownTime;
                    StartCoroutine("DashMove");
                    StartCoroutine(cameraShake.Shake(.10f, .3f));
                }
            }
        }
    }

    if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName ("Level5-4"))
    {
        dashEnabled = false;
        isCamShakeEnabled = false;
    }

    if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("Level5-4"))
    {
        isCamShakeEnabled = false;
    }
}



void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.transform.CompareTag("killer"))
        gameObject.GetComponent<TrailRenderer>().enabled = false;
}




IEnumerator DashMove()
{
    playerSpeed += 60;
    particleLauncher.Emit(30);
    yield return new WaitForSeconds(.07f);
    playerSpeed -= 60;
}


void OnCollisionEnter2D(Collision2D other)
{
    if (other.gameObject.CompareTag("Ground"))
    {
        isJumping = false;
        gameObject.GetComponent<TrailRenderer>().enabled = true;
    }

    if (other.gameObject.CompareTag("BK"))
    {
        isJumping = false;
    }

    if (other.gameObject.CompareTag("MGround"))
    {
        this.transform.parent = other.transform;
        isJumping = false;
    }

    if (other.gameObject.CompareTag("Mcircle"))
    {
        this.transform.parent = other.transform;
        isJumping = false;
    }
}

void OnCollisionExit2D(Collision2D other)
{
    if (other.gameObject.CompareTag("MGround"))
    {
        this.transform.parent = null;
        isJumping = true;
    }

    if (other.gameObject.CompareTag("Mcircle"))
    {
        this.transform.parent = null;
        isJumping = true;
    }

    if (other.gameObject.CompareTag("Ground"))
    {
        isJumping = true;
    }

    if (other.gameObject.CompareTag("BK"))
    {
        isJumping = false;
    }
}

private void Flip(float move)
{
    if (move > 0 && !facingRight || move < 0 && facingRight)
    {
        facingRight = !facingRight;

        Vector3 theScale = transform.localScale;

        theScale.x *= -1;

        transform.localScale = theScale;
    }
}
\$\endgroup\$
  • \$\begingroup\$ Are you lagging in editor mode or after building it? Also, can you provide a gif sample of your problem? \$\endgroup\$ – A.Gerber Jun 29 at 11:54
  • \$\begingroup\$ When you have to troubleshoot performance problems in Unity, then it is usually good to know how to use the Profiler. \$\endgroup\$ – Philipp Jun 29 at 11:55
  • \$\begingroup\$ One more thing. I would put all the movement code in fixedUpdate function, not in both update and fixedUpdate. \$\endgroup\$ – A.Gerber Jun 29 at 11:55
  • \$\begingroup\$ It's not bad to put instantaneous actions like the start of a dash in Update (though you should use StartCoroutine(DashMove()) rather than using reflection to get the method by name). Can you confirm how you've configured your character's Rigidbody interpolation, and any follow camera logic? These are common places to introduce judder that looks superficially similar to a framerate stutter. \$\endgroup\$ – DMGregory Jun 29 at 12:10
  • 1
    \$\begingroup\$ @DMGregory for scenes i use camera follow i store all script in late update and it only follows the player on the x axis by using the .x function on a vector3 called temp, so temp.x = the transform of the player's position.x \$\endgroup\$ – bblakeyyy Jun 29 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.