Build feeling jittering/laggy

Hello, I’m making a 2D game for Android, right now it’s really simple but yet it feels laggy when creating a build. I’ve followed a few tutorials, lowered quality, etc. but I feel like I’m missing something.
This is my code for moving my character (you can drag and shoot something like Angry Birds):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Controls : MonoBehaviour
{
    public float power = 10f;
    public float maxDrag = 5f;
    public Rigidbody2D rb;
    public LineRenderer lr;
    public SpriteRenderer sr;
    public AudioClip audioclip;
    public int jumps = 1;
    Vector3 dragStartPos;
    Touch touch;
    private void Start(){
        rb.velocity = new Vector2(0.0f, 0.0f);
        Time.timeScale = 0;
    }
    private void Update()
    {
        if(jumps >= 1){
            sr.color = new Color(0.9433962f, 0.493948f, 0.943103f, 1f);
        if(Input.touchCount > 0)
        {
            touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Began)
            {
                DragStart();
            }
            if (touch.phase == TouchPhase.Moved)
            {
                Dragging();
            }
            if (touch.phase == TouchPhase.Ended)
            {
                DragRealease();
            }

           
        }
        }
        if(jumps == 0){
            sr.color = new Color(1f, 1f, 1f, 1f);
        }

      
    }

     private void OnTriggerEnter2D(Collider2D other){
        if(other.CompareTag("Surface")){
            Vector3 playerPos=other.transform.position;
            rb.velocity = new Vector2(0.0f, 0.0f);
            rb.gravityScale = 1f;
            Debug.Log(playerPos.x);
            jumps = 1;
            ScoreScript.scoreValue+=1;
        }

        if(other.CompareTag("Finish")){
            //SceneManager.LoadScene("GameOver");
            jumps=1;
            GetComponent<AudioSource>().clip = audioclip;
            GetComponent<AudioSource>().Play();
        }

        if (other.CompareTag("Coin")){
            //coins.coinsvalue+=1;
            Destroy(other.gameObject);
        }

         if (other.CompareTag("Win"))
        {
            if (changer.canwin)
            {
                SceneManager.LoadScene("Win");
                ScoreScript.scoreValue = 0;
               
                //coins.coinsvalue = 0;
            } /*else
            {
                SceneManager.LoadScene("GameOver");
                ScoreScript.scoreValue = 0;
                JUMPS.jumpsleft = 1;
                coins.coinsvalue = 0;
            }*/
        }
    }

    private void DragStart()
    {
        dragStartPos = Camera.main.ScreenToWorldPoint(touch.position);
        dragStartPos.z = 0f;
        lr.positionCount = 1;
        lr.SetPosition(0, dragStartPos);
    }
    private void Dragging()
    {
        Vector3 draggingPos = Camera.main.ScreenToWorldPoint(touch.position);
        dragStartPos.z = 0f;
        lr.positionCount = 2;
        lr.SetPosition(1, draggingPos);
    }
    private void DragRealease()
    {
        Time.timeScale = 1;
        lr.positionCount = 0;

        Vector3 dragReleasePos = Camera.main.ScreenToWorldPoint(touch.position);
        dragStartPos.z = 0f;

        Vector3 force = dragStartPos - dragReleasePos;
        Vector3 clampedForce = Vector3.ClampMagnitude(force, maxDrag) * power;
        rb.AddForce(clampedForce, ForceMode2D.Impulse);
        jumps = 0;
    }
}

I’ve read that I should do transforms and etc. in FixedUpdate instead of Update but when I tried to, the game would instantly freeze, not allowing to play.

You’re not using “Transforms”, you’re setting a force on a Rigidbody2D and letting it (as it should) write to the Transform.

Your code doesn’t make sense to me though, you’re applying an impulse only when you release, not while you’re dragging the mouse. That’ll just cause it to fly off in the direction of the drag and only do so when you release.

There’s a much simpler way of doing physics-based dragging which you can see in the following video with links to the GutHub project here:

Essentially it’s using the TargetJoint2D for this.

An even simpler method is to simply use Rigidbody2D.MovePosition while you’re dragging (in the “Dragging” method). This also won’t leave you with unwanted velocity at all.

NOTES:

  • In the start method you’re doing questionable stuff. You’re setting the global time-scale to zero. This affects everything in Unity. I would not do that. Also, setting the velocity of a Rigidbody2D to zero here is redundant, that’s the default anyway.
  • You’re using Vector3.ClampMagnitude. Use Vector2.ClampMagnitude where possible and convert to Vector2 instead. The Z value can cause subtle bugs if you’re not extremely careful.
1 Like

The velocity is intended as is the character flying off in the direction of the drag. The example that you’ve shown in the video is not quite the thing I was aiming for. I was trying to make something more like this:

As for the global time-scale to zero, my intention was to freeze the game, before the first touch (because I wanted the ball that I’m controlling to float in the air when the level begins, but also have gravity applied to it at the same time).

Ah right, sorry. For some reason I didn’t register the angry birds you mentioned!

You say “build” but what part is jittery? Are you aware that physics (by default) runs during the FixedUpdate? The FixedUpdate by default runs at 50Hz as set in the time-manager. This means, any physics update doesn’t happen per-frame. This is why a Rigidbody2D has an interpolate option that causes the Transform to move from the old body position to the new (current) body position per-frame providing smooth visuals. Maybe you have this option off?

BTW, a simpler assignment is to just set the body velocity rather than using an impulse. It’ll be the same thing except an impulse is scaled by the mass of the body.

1 Like

YES! The interpolate option was the thing. Now the game feels smooth, thank you a lot, you are a lifesaver!

1 Like