Answer the question
In order to leave comments, you need to log in
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
. 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");
}
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;
}
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
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 questionAsk a Question
731 491 924 answers to any question