I have a parent object that has to stop depending on the position of its children. I need the first and only children to stop at say, -10y. However, with the example I will provide, this is not happening. Instead it seems all children have to be below this point.
The two primary scripts use are as follows:
public class Tetro:MonoBehaviour
{
Instantiator instance;
void Start ()
{
instance = FindObjectOfType<Instantiator>();
StartCoroutine(Fall());
}
IEnumerator Fall()
{
foreach(Transform mino in transform)
{
while(mino.position.y > -10)
{
yield return new WaitForSeconds(1);
transform.position -= new Vector3(0, 1);
}
}
StopCoroutine(Fall());
}
public void CheckInput()
{
if (transform.position.y > -10)
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.position += new Vector3(1, 0);
if (!checkEachChildPosition())
{
transform.position += new Vector3(-1, 0);
}
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.position += new Vector3(-1, 0);
if (!checkEachChildPosition())
{
transform.position += new Vector3(1, 0);
}
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
transform.Rotate(0, 0, 90);
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
transform.position -= new Vector3(0, 1);
if (!checkEachChildPosition())
{
transform.position += new Vector3(0, 1);
}
}
}
}
bool checkEachChildPosition()
{
foreach (Transform mino in transform)
{
Vector2 pos = FindObjectOfType<GameManager.GameManager>().RoundOffPositions(mino.position);
if (FindObjectOfType<GameManager.GameManager>().CheckIsInGrid(pos) == false)
{
return false;
}
}
return true;
}
void AllowNext()
{
if (transform.position.y == -10)
{
instance.Spawn();
enabled = false;
}
}
void Update ()
{
CheckInput();
AllowNext();
}
}
And:
namespace GameManager
{
public class GameManager:MonoBehaviour
{
Instantiator instance;
void Start()
{
instance = FindObjectOfType<Instantiator>();
instance.Spawn();
}
public bool CheckIsInGrid(Vector2 pos)
{
return (pos.x >= -7 && pos.x <= 7 && pos.y > -10 && pos.y < 12);
}
public Vector2 RoundOffPositions(Vector2 pos)
{
return new Vector2(Mathf.Round(pos.x), Mathf.Round(pos.y));
}
void Update()
{
}
}
}
I assume the issue is occurring in Fall()
but it shouldn’t. Anyone know why this is happening? Or how this can be fixed?