Hi guys I’m trying to do a 2d-game where it uses tetris shapes. I’m having problems with the collision. And this is what i did.
-
Create a cube. Set size to be 64x64. Check (tick/enable) the “IsTrigger” in box collision.
-
Create a empty game object and drag the cube into this empty game object
-
Copy the cube and creates 3 other instances of that cube. Arrange it to make an inverted “L” shape.
—
—
← gameobjects position.
Now i want to make it such that the box collider follows this inverted “L” shape. How do i do that? Oh and here is my script which i attached to my gameobject
// Update is called once per frame
void Update () {
// if falling is allowed
if (m_bIsFalling)
{
Fall();
}
}
void Fall()
{
this.transform.position = new Vector3( this.transform.position.x, this.transform.position.y - Falling_Speed, this.transform.position.z );
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Ground" || other.gameObject.tag == "Wall")
{
this.transform.position = new Vector3(other.gameObject.transform.position.x ,this.transform.position.y, this.transform.position.z);
m_bIsFalling = false;
}
}
which was initially attached to each individual cube with this code instead:
// Update is called once per frame
void Update () {
// if falling is allowed
if (m_bIsFalling)
{
Fall();
}
}
void Fall()
{
this.transform.Translate(new Vector3( 0, - Falling_Speed, 0 ) );
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Ground" || other.gameObject.tag == "Wall" || other.gameObject.tag == "TPBlock")
{
this.transform.position.Set(other.gameObject.transform.position.x ,this.transform.position.y, this.transform.position.z);
m_bIsFalling = false;
}
}
I also tried using fixed hinges to make the 4 cubes act as 1 object but that failed miserably. Can anyone help me here? Any help is appreciated. Thanks in advance.
The structure of this sounds confusing, but to answer the question in the title, yes you CAN access children of a gameobject. In a class in the parent you can access children using: foreach(Transform child in transform) { Collider childCollider = child.GetComponent<Collider>(); // do stuff with the collider }
– liamcaryso how do i call the OnTriggerEnter function inside there? for eg. in the OnTriggerEnter function for each individual child i want to put void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "Ground" || other.gameObject.tag == "Wall" || other.gameObject.tag == "TPBlock") { //this.transform.position = new Vector3(other.gameObject.transform.position.x ,this.transform.position.y, this.transform.position.z); m_bIsFalling = false; } } where it checks the tag of the collider and see if its "Ground" or whatever.
– mh4and thanks for helping me out. much appreciated
– mh4Thanks alot Liamcary! You've been such a great help! Many thanks!
– mh4