0
\$\begingroup\$

within 10-20 min without touching the android game it would lag. Here is the Unity Profiler Debug.

enter image description here

This if the fish clicker script where I started noticing lag.

public class FishClicker : MonoBehaviour
{
[SerializeField]
private LayerMask whatIsFish;

[SerializeField]
private float clickRadius = 0.1f;

[SerializeField]
private int coloredFish;
void Update()
{
    var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

    if (Input.GetMouseButtonDown(0))
    {
        var hit = Physics2D.OverlapCircle(mousePos, clickRadius, whatIsFish);

        if (hit != null)
        {
            if (hit.name == "Blue_Fish(Clone)")
            {
                var fish = hit.GetComponentInParent<BlueFishOne>();

                // fish.DoClickThing();
                if (fish != null) 
                {
                    fish.ControlFishClick();
                    coloredFish = 1;
                    //sprint(fish.name);
                }
            }
            else if (hit.name == "Green_Fish(Clone)")
            {
                var fish = hit.GetComponentInParent<GreenFishOne>();
                if (fish != null)
                {
                    fish.ControlFishClick();
                    coloredFish = 2;
                    //print(fish.name);
                }
            }
            else if (hit.name == "Indigo_Fish(Clone)")
            {
                var fish = hit.GetComponentInParent<IndigoFishOne>();
                
                if (fish != null)
                {
                    fish.ControlFishClick();
                    coloredFish = 3;
                    //print(fish.name);
                }
            }
            else if (hit.name == "Orange_Fish(Clone)")
            {
                var fish = hit.GetComponentInParent<OrangeFishOne>();

                if (fish != null)
                {
                    fish.ControlFishClick();
                    coloredFish = 4;
                    //print(fish.name);
                }
            }
            else if (hit.name == "Red_Fish(Clone)")
            {
                var fish = hit.GetComponentInParent<RedFishOne>();

                if (fish != null)
                {
                    fish.ControlFishClick();
                    coloredFish = 5;
                    //print(fish.name);
                }
            }
            else if (hit.name == "Violet_Fish(Clone)")
            {
                var fish = hit.GetComponentInParent<VioletFishOne>();

                if (fish != null)
                {
                    fish.ControlFishClick();
                    coloredFish = 6;
                    //print(fish.name);
                }
            }
            else if (hit.name == "Yellow_Fish(Clone)")
            {
                var fish = hit.GetComponentInParent<YellowFishOne>();

                if (fish != null)
                {
                    fish.ControlFishClick();
                    coloredFish = 7;
                    //print(fish.name);
                }
            }
        }
    }

    if (Input.GetMouseButton(0))
    {
        var hit = Physics2D.OverlapCircle(mousePos, clickRadius, whatIsFish);

        if (hit != null)
        {
            if (coloredFish == 1)
            {
                if (hit.name == "Blue_Fish(Clone)")
                {
                    var fish = hit.GetComponentInParent<BlueFishOne>();

                    if (fish != null)
                    {
                        fish.ControlFishDrag();
                    }
                }
            }
            else if (coloredFish == 2)
            {
                if (hit.name == "Green_Fish(Clone)")
                {
                    var fish = hit.GetComponentInParent<GreenFishOne>();

                    if (fish != null)
                    {
                        fish.ControlFishDrag();
                    }
                }
            }
            else if (coloredFish == 3)
            {
                if (hit.name == "Indigo_Fish(Clone)")
                {
                    var fish = hit.GetComponentInParent<IndigoFishOne>();

                    if (fish != null)
                    {
                        fish.ControlFishDrag();
                    }
                }
            }
            else if (coloredFish == 4)
            {
                if (hit.name == "Orange_Fish(Clone)")
                {
                    var fish = hit.GetComponentInParent<OrangeFishOne>();

                    if (fish != null)
                    {
                        fish.ControlFishDrag();
                    }
                }
            }
            else if (coloredFish == 5)
            {
                if (hit.name == "Red_Fish(Clone)")
                {
                    var fish = hit.GetComponentInParent<RedFishOne>();

                    if (fish != null)
                    {
                        fish.ControlFishDrag();
                    }
                }
            }
            else if (coloredFish == 6)
            {
                if (hit.name == "Violet_Fish(Clone)")
                {
                    var fish = hit.GetComponentInParent<VioletFishOne>();

                    if (fish != null)
                    {
                        fish.ControlFishDrag();
                    }
                }
            }
            else if (coloredFish == 7)
            {
                if (hit.name == "Yellow_Fish(Clone)")
                {
                    var fish = hit.GetComponentInParent<YellowFishOne>();

                    if (fish != null)
                    {
                        fish.ControlFishDrag();
                    }
                }
            }
        }
    }

    if (Input.GetMouseButtonUp(0))
    {
        coloredFish = 0;
    }
}

}

This is the fish script for each prefab. BlueFishOne script is for blue fish prefabs and RedFishOne script is for red fish prefabs etc.

public class BlueFishOne : MonoBehaviour
{
//public static BlueFishOne instance;

Animator animate;

private Rigidbody2D rigid2D;
private BoxCollider2D box2D;

private Camera myCam;
private Vector3 screenPos;
private float angleOffset;

[SerializeField]
private float speedX, speedY, diveTimer, diveReached,
              clickRadius = 0.1f;

[SerializeField]
private int fishCount = 0, fishColorIs;

[SerializeField]
private bool stopMovement;

[SerializeField]
private LayerMask whatIsFish;

// Start is called before the first frame update
void Start()
{
    //instance = this;

    animate = GetComponent<Animator>();

    rigid2D = GetComponent<Rigidbody2D>();
    box2D = GetComponent<BoxCollider2D>();
    myCam = Camera.main;

    box2D.enabled = false;
    box2D.enabled = true;
    
    var degrees = Random.Range(0, 361);
    transform.eulerAngles = new Vector3(0, 0, degrees);

    diveReached = Random.Range(1, 4);

    //Physics2D.IgnoreCollision(algae.GetComponent<CircleCollider2D>(), GetComponent<BoxCollider2D>());
}

// Update is called once per frame
void FixedUpdate()
{
    //Diving();
    FishCount();

    //Movement();

    if (!stopMovement)
    {
        Movement();
    }
    else
    {
        rigid2D.velocity = new Vector2(0, 0);
    }

    if (Input.GetMouseButtonDown(0))
    {
        stopMovement = false;
        SnapToPosition();
    }

    if (Input.GetMouseButtonUp(0))
    {
        stopMovement = false;
        SnapToPosition();
    }

    //print(stopMovement);
}

public void ControlFishClick()
{
    //stopMovement = true;
    screenPos = myCam.WorldToScreenPoint(transform.position);
    Vector3 v3 = Input.mousePosition - screenPos;
    angleOffset = (Mathf.Atan2(transform.right.y, transform.right.x) - Mathf.Atan2(v3.y, v3.x)) * Mathf.Rad2Deg;

}

public void ControlFishDrag()
{
    stopMovement = true;
    //rigid2D.velocity = new Vector2(0, 0);
    Vector3 v3 = Input.mousePosition - screenPos;
    float angle = Mathf.Atan2(v3.y, v3.x) * Mathf.Rad2Deg;
    transform.eulerAngles = new Vector3(0, 0, angle + angleOffset);
    //print(stopMovement);
}

public void ControlFishUp()
{
    stopMovement = false;
    SnapToPosition();
}

void Diving()
{
    diveTimer += Time.fixedDeltaTime;

    if (diveTimer > diveReached)
    {
        GameManager.instance.Dive(animate, "Dive");
    }
}

void FishCount()
{
    if (FishSpawner.instance.fishCount > 0 && fishCount == 0)
    {
        fishCount = FishSpawner.instance.fishCount;
    }
}

void Dive()
{
    animate.gameObject.GetComponent<Animator>().enabled = false;
    //this.gameObject.SetActive(false);
    //this.gameObject.transform.position = new Vector3(GameManager.instance.xPos, GameManager.instance.yPos);

    Destroy(this.gameObject);
}

void Movement()
{
    float horizontal = speedX * Time.deltaTime;
    float vertical = speedY * Time.deltaTime;

    // 0 degree
    if (transform.eulerAngles.z < 22.5f && transform.eulerAngles.z >= 0 ||
        transform.eulerAngles.z < 360.0f && transform.eulerAngles.z >= 337.5f)
    {
        //transform.eulerAngles = new Vector3(0, 0, 0);
        rigid2D.velocity = new Vector2(horizontal, 0);
    }

    // 45 degree
    if (transform.eulerAngles.z < 67.5f && transform.eulerAngles.z >= 22.5f)
    {
        //transform.eulerAngles = new Vector3(0, 0, 45.0f);
        rigid2D.velocity = new Vector2(horizontal, vertical);
    }

    // 90 degree
    if (transform.eulerAngles.z < 112.5f && transform.eulerAngles.z >= 67.5f)
    {
        //transform.eulerAngles = new Vector3(0, 0, 90.0f);
        rigid2D.velocity = new Vector2(0, vertical);
    }

    // 135 degree
    if (transform.eulerAngles.z < 157.5f && transform.eulerAngles.z >= 112.5f)
    {
        //transform.eulerAngles = new Vector3(0, 0, 135.0f);
        rigid2D.velocity = new Vector2(-horizontal, vertical);
    }

    // 180 degree
    if (transform.eulerAngles.z < 202.5f && transform.eulerAngles.z >= 157.5f)
    {
        //transform.eulerAngles = new Vector3(0, 0, 180.0f);
        rigid2D.velocity = new Vector2(-horizontal, 0);
    }

    // 225 degree
    if (transform.eulerAngles.z < 247.5f && transform.eulerAngles.z >= 202.5f)
    {
        //transform.eulerAngles = new Vector3(0, 0, 225.5f);
        rigid2D.velocity = new Vector2(-horizontal, -vertical);
    }

    // 270 degree
    if (transform.eulerAngles.z < 292.5f && transform.eulerAngles.z >= 247.5f)
    {
        //transform.eulerAngles = new Vector3(0, 0, 270.0f);
        rigid2D.velocity = new Vector2(0, -vertical);
    }

    // 315 degree
    if (transform.eulerAngles.z < 337.5f && transform.eulerAngles.z >= 292.5f)
    {
        //transform.eulerAngles = new Vector3(0, 0, 315.0f);
        rigid2D.velocity = new Vector2(horizontal, -vertical);
    }
}

void SnapToPosition()
{
    // 0 degree
    if (transform.eulerAngles.z < 22.5f && transform.eulerAngles.z >= 0 ||
        transform.eulerAngles.z < 360.0f && transform.eulerAngles.z >= 337.5f)
    {
        transform.eulerAngles = new Vector3(0, 0, 0);
        //rigid2D.velocity = new Vector2(horizontal, 0);
    }

    // 45 degree
    if (transform.eulerAngles.z < 67.5f && transform.eulerAngles.z >= 22.5f)
    {
        transform.eulerAngles = new Vector3(0, 0, 45.0f);
        //rigid2D.velocity = new Vector2(horizontal, vertical);
    }

    // 90 degree
    if (transform.eulerAngles.z < 112.5f && transform.eulerAngles.z >= 67.5f)
    {
        transform.eulerAngles = new Vector3(0, 0, 90.0f);
        //rigid2D.velocity = new Vector2(0, vertical);
    }

    // 135 degree
    if (transform.eulerAngles.z < 157.5f && transform.eulerAngles.z >= 112.5f)
    {
        transform.eulerAngles = new Vector3(0, 0, 135.0f);
        //rigid2D.velocity = new Vector2(-horizontal, vertical);
    }

    // 180 degree
    if (transform.eulerAngles.z < 202.5f && transform.eulerAngles.z >= 157.5f)
    {
        transform.eulerAngles = new Vector3(0, 0, 180.0f);
        //rigid2D.velocity = new Vector2(-horizontal, 0);
    }

    // 225 degree
    if (transform.eulerAngles.z < 247.5f && transform.eulerAngles.z >= 202.5f)
    {
        transform.eulerAngles = new Vector3(0, 0, 225.5f);
        //rigid2D.velocity = new Vector2(-horizontal, -vertical);
    }

    // 270 degree
    if (transform.eulerAngles.z < 292.5f && transform.eulerAngles.z >= 247.5f)
    {
        transform.eulerAngles = new Vector3(0, 0, 270.0f);
        //rigid2D.velocity = new Vector2(0, -vertical);
    }

    // 315 degree
    if (transform.eulerAngles.z < 337.5f && transform.eulerAngles.z >= 292.5f)
    {
        transform.eulerAngles = new Vector3(0, 0, 315.0f);
        //rigid2D.velocity = new Vector2(horizontal, -vertical);
    }

}
}

This is the fish food script.

public class Algae : MonoBehaviour
{
[SerializeField]
private SpriteRenderer sprite;

[SerializeField]
private CircleCollider2D cir;

[SerializeField]
private float rotationSpeed = 0f, rotTimer, rotTimeReached,
              move, speed, rotateSpeed = 5f, radius = 0.1f, _angle;

[SerializeField]
private bool rotRight = false, xAxis, yAxis;

[SerializeField]
private Vector2 _centre;

[SerializeField]
private int algaeColorIs;

// Start is called before the first frame update
void Start()
{
    sprite = GetComponent<SpriteRenderer>();
    cir = GetComponent<CircleCollider2D>();
    transform.position = new Vector3(Random.Range(-8.5f, 8.5f), Random.Range(-4.5f, 4.5f));
   
    
}

// Update is called once per frame
void Update()
{
    Spin();
    Movement();
}

void Movement()
{
    Mathf.Clamp(transform.position.x, -8.5f, 8.5f);
    Mathf.Clamp(transform.position.y, -4.5f, 4.5f);

    move = speed * Time.deltaTime; // calculate distance to move
    if (transform.position.x <= -8.5f)
    {
        xAxis = true;
    }
    else if (transform.position.x >= 8.5f)
    {
        xAxis = false;
    }

    if (transform.position.y <= -4.5f)
    {
        yAxis = true;
    }
    else if (transform.position.y >= 4.5f)
    {
        yAxis = false;
    }

    if (xAxis)
    {
        transform.position = Vector3.MoveTowards(transform.position, new Vector3(8.5f, transform.position.y), move);
    }
    else if (!xAxis)
    {
        transform.position = Vector3.MoveTowards(transform.position, new Vector3(-8.5f, transform.position.y), move);
    }

    if (yAxis)
    {
        transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x, 4.5f), move);
    }
    else if (!yAxis)
    {
        transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x, -4.5f), move);
    }


}

void Spin()
{
    rotTimer += Time.deltaTime;

    if (rotTimer >= rotTimeReached)
    {
        rotRight = !rotRight;
        rotTimer = 0f;
    }

    if (rotRight)
    {
        transform.eulerAngles -= new Vector3(0f, 0f, Time.deltaTime * rotationSpeed);
    }
    else if (!rotRight)
    {
        transform.eulerAngles += new Vector3(0f, 0f, Time.deltaTime * rotationSpeed);
    }
}

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "BlueFish" && 
        this.gameObject.tag == "BlueAlgae")
    {
        this.gameObject.transform.position = 
            new Vector2(
                Random.Range(-11.5f,-9.5f),Random.Range(-7.5f, 7.5f)
                );
    }
    else if (other.tag == "GreenFish" &&
        this.gameObject.tag == "GreenAlgae")
    {
        this.gameObject.transform.position =
            new Vector2(
                Random.Range(-11.5f, -9.5f), Random.Range(-7.5f, 7.5f)
                );
    }
    else if (other.tag == "IndigoFish" &&
        this.gameObject.tag == "IndigoAlgae")
    {
        this.gameObject.transform.position =
            new Vector2(
                Random.Range(-11.5f, -9.5f), Random.Range(-7.5f, 7.5f)
                );
    }
    else if (other.tag == "OrangeFish" &&
        this.gameObject.tag == "OrangeAlgae")
    {
        this.gameObject.transform.position =
            new Vector2(
                Random.Range(-11.5f, -9.5f), Random.Range(-7.5f, 7.5f)
                );
    }
    else if (other.tag == "RedFish" &&
        this.gameObject.tag == "RedAlgae")
    {
        this.gameObject.transform.position =
            new Vector2(
                Random.Range(-11.5f, -9.5f), Random.Range(-7.5f, 7.5f)
                );
    }
    else if (other.tag == "VioletFish" &&
        this.gameObject.tag == "VioletAlgae")
    {
        this.gameObject.transform.position =
            new Vector2(
                Random.Range(-11.5f, -9.5f), Random.Range(-7.5f, 7.5f)
                );
    }
    else if (other.tag == "YellowFish" &&
        this.gameObject.tag == "YellowAlgae")
    {
        this.gameObject.transform.position =
            new Vector2(
                Random.Range(-11.5f, -9.5f), Random.Range(-7.5f, 7.5f)
                );
    }
}
}
\$\endgroup\$
  • 1
    \$\begingroup\$ This piece of code should be fine, but it is calling doclickthing which could potentially cause the issue. Did you run a profiler to see what exactly is calling it to lag? \$\endgroup\$ – Zibelas Nov 15 at 7:01
  • \$\begingroup\$ Thank you for the response, I left it running 10-20 min without touching the screen and it still lagged. I never used Unity profiler i'll have to read into that. \$\endgroup\$ – MM1Content Nov 15 at 7:40
  • \$\begingroup\$ In the Unity Profiler between 10-20 min in CPU Usage the Physics color is huge! 2/3rd the animation color is 1/3rd and garbage collector is tiny. \$\endgroup\$ – MM1Content Nov 15 at 7:56
  • \$\begingroup\$ My bet is that you're moving a complex collider or many using transforms rather than rigidbodies. Ask about your real problem "How do I solve this physics slowdown" not your guessed-at solution "get rid of layers". Layers are generally good for efficiency by letting the engine reduce the number of candidate bodies to process for a particular event. Edit your question to include your profiling information, and a view of how these fish are set up — their scripts, colliders, Rigidbody, visual appearance, etc. \$\endgroup\$ – DMGregory Nov 15 at 10:38
  • \$\begingroup\$ I appreciate it. \$\endgroup\$ – MM1Content Nov 15 at 18:30

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.