W
W
WeBBeW2020-04-20 18:22:05
C++ / C#
WeBBeW, 2020-04-20 18:22:05

The unit hangs tightly. What should I do?

I'm making a simple 2d game. When I start the game (in the unit itself) everything is fine, it doesn’t lag, but if you start playing (well, in my case you click on the cube, it jumps), the cube bounces, flies and after a few seconds the unit freezes tightly. What's causing this? RAM is only 50% full. When the code was written, everything was fine, but I added a few more lines and after that the unit started to freeze

Here is the code (maybe because of this?):

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

public class CubeJump : MonoBehaviour
{
    public GameObject mainCube;
    private bool animate, nextBlock = true, lose;
    private float scratch_speed = 0.5f, startTime, yPosCube;

    void FixedUpdate()
    {
         if (animate && mainCube.transform.localScale.y > 0.4f)
         {
            PressCube(-scratch_speed * 3f);
         }
        else if (!animate && mainCube != null)
            {
                if (mainCube.transform.localScale.y < 1f)
                {
                    PressCube(scratch_speed * 3f);
                }
            else if(mainCube.transform.localScale.y != 1f)
                {
                    mainCube.transform.localScale = new Vector3(1f, 1f, 1f);
                }
            }
        if (mainCube != null)
        {
            if (mainCube.transform.localPosition.y < -8.5f)
            {
                Destroy(mainCube, 1f);
                print("lose");
                lose = true;
            }
        }
    }

    void OnMouseDown()
    {
        if(nextBlock && mainCube.GetComponent<Rigidbody>())
        {
            animate = true;
            startTime = Time.time;
        }

        yPosCube = mainCube.transform.localPosition.y;
    }

    
    void OnMouseUp()
    {
        if (nextBlock && mainCube.GetComponent<Rigidbody>())
        {
            animate = false;

            //Jump
            float force, diff;
            diff = Time.time - startTime;
            if (diff < 3f)
                force = 190 * diff;
            else
                force = 300f;
            if (force < 60)
                force = 60;
            mainCube.GetComponent<Rigidbody>().AddRelativeForce(mainCube.transform.up * force);
            mainCube.GetComponent<Rigidbody>().AddRelativeForce(mainCube.transform.right * force);

            StartCoroutine(checkCubePos());
            nextBlock = false;
        }
    }

    void PressCube (float force)
    {
        mainCube.transform.localPosition += new Vector3(0f, force * Time.deltaTime, 0f);
        mainCube.transform.localScale += new Vector3(0f, force * Time.deltaTime, 0f);
    }

    IEnumerator checkCubePos ()
    {
        yield return new WaitForSeconds(1.5f);          
        if(yPosCube == mainCube.transform.localPosition.y)
        { 
            print("playes lose");
            lose = true;
        }else {
            while (!mainCube.GetComponent<Rigidbody>().IsSleeping())
                if (mainCube == null)
                    break;
            yield return new WaitForSeconds(0.05f);
            if (!lose)
            {
                nextBlock = true;
                print("Next one");
                mainCube.transform.localPosition = new Vector3(-0.3f, mainCube.transform.localPosition.y, mainCube.transform.localPosition.z);
                mainCube.transform.eulerAngles = new Vector3(0f, mainCube.transform.eulerAngles.y, 0);
            }
        }
    }
}


It seems like when something was added to IEnumerator checkCubePos () and it started to freeze

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question