W
W
WeBBeW2020-06-24 20:29:51
C++ / C#
WeBBeW, 2020-06-24 20:29:51

Doesn't check if a cube is on a cube. How to solve the problem?

I'm making a simple game.
The game looks something like the picture
5ef389df9ec30943755934.jpeg
. And I have a problem. I need to check if the created cube is on the cube?

Here is the code where check for it

if(!SpawnCubik.created2cube)
                    StartCubeFell = true;//следующий кубик

                //print("start");

                if (SpawnCubik.CubikCreated)// проверка на создание кубика
                {

                    SpawnCubik.newNextCubeInst.GetComponent<Rigidbody>().isKinematic = false;//чтобы упало

 вот проверка -> if (SpawnCubik.newNextCubeInst.transform.position.y < (SpawnCubik.StartCoordY + SpawnCubik.successfullyFellCubik))
                    {//если упало не на куб, то игра завершена
                         print(SpawnCubik.newNextCubeInst.transform.position.y);
                         print(SpawnCubik.StartCoordY + 1f * SpawnCubik.successfullyFellCubik);
                         print("end");
                         StartCubeFell = false;
                         SpawnCubik.CubikCreated = false;
                         NotGameStartedClicCubik = true;
                    }
                    else//если упало на куб, то создаем другой куб
                    {
                         SpawnCubik.CubikCreated = false;//для создания другого кубика
                         StartCubeFell = true;
                         SpawnCubik.successfullyFellCubik++;
                         print("4");
                    }


Code where cubes are created:
if(ClickToPlay.StartCubeFell && !CubikCreated && created2cube)
        {
            newNextCubeInst = Instantiate(prefabCube, new Vector2(-5.18f, StartCoordY +  successfullyFellCubik), Quaternion.identity) as GameObject;
            newNextCubeInst.transform.SetParent(parent);
            CubikCreated = true;
            created2cube = true;
        }

        else if (ClickToPlay.StartCubeFell && !CubikCreated)
        {
            newNextCubeInst = Instantiate(prefabCube, new Vector2(-5.18f, StartCoordY +  successfullyFellCubik), Quaternion.identity) as GameObject;
            newNextCubeInst.transform.SetParent(parent);
            CubikCreated = true;
        }


successfullyFellCubik - if the created cube fell on the cube, then this value is increased by 1, but since the check does not work, then this is immediately performed:
else//если упало на куб, то создаем другой куб
                    {
                         SpawnCubik.CubikCreated = false;//для создания другого кубика
                         StartCubeFell = true;
                         SpawnCubik.successfullyFellCubik++;
                         print("4");
                    }
and each click just increments successfullyFellCubik by 1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zZaKko, 2020-06-24
@WeBBeW

If I understand correctly, what you want to do, then do it like this:
Write the built-in function OnCollisionEnter2D(OnTriggerEnter2D) and when the created cube touches the already standing cube when it falls, then compare their positions along the y vector.
Here's how it should be approximately:
void OnCollisionEnter2D(Collision2D coll){
if(coll.gameobject.tag == lastcube.tag){
if(coll.gameobject.transform.position.y > lastcube.transform.position.y)
your code
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question