Falling blocks! (Answered)

I just started with unity not too long ago.
I want to set up a block that, after the player has touched, it will drop in 3 seconds.
I have a cube set up with a box collider and a rigidbody (nothing checked) and a childed empty game object with a box collider set to “is trigger” and a script as follows.


using UnityEngine;
using System.Collections;

public class FallingCube : MonoBehaviour {
	
	// Use this for initialization
	void Start () {}
	
	// Update is called once per frame
	void Update () {}
	
	IEnumerable OnCollision(){
		Debug.Log("Collision", this);
		yield return new WaitForSeconds(3.0f);
		StartCoroutine("CubeFalling");
	}	
	
	IEnumerable OnTriggerEnter(){
		Debug.Log("Trigger", this);
		yield return new WaitForSeconds(3.0f);
		StartCoroutine("CubeFalling");
	}
	
	void CubeFalling(){
		transform.parent.rigidbody.useGravity = true;
	}
}

I just can’t figure out how to set it up properly…

//UPDATE
The trigger is firing, now that I have replaced the IEnumerable with void and removed the wait. For some reason the trigger will not call the other function. So, I put the gravity on code inside the OnTrigger. Now, it’s triggering on start of the game…

No, the debug lines aren't showing up. I'm not sure which object needs the script attached, so I attached it to the empty game-object. A comment below says to make sure the trigger is large enough, so I'm going to try that now. No luck - the blocks just spin out of the way like in zero gravity, no pause either and no debug statements.

If the question is answered, mark it as such.

I can't figure out how...

Click on the check mark.

3 Answers

3

I think you got mixed up between your methods. This is tested and works.

	IEnumerator CubeFalling ()
	{
		yield return new WaitForSeconds(3.0f);
		transform.parent.rigidbody.useGravity = true;
		// You could also call your method here and delete the line above
		// CubeFall();
	}
	
	void OnCollisionEnter (Collision col)
	{
		Debug.Log("Hit");
		StartCoroutine ("CubeFalling");
	}
	
	// Optional
	void CubeFall ()
	{
		transform.parent.rigidbody.useGravity = true;
	}

OnCollisionEnter will not work in this case, because the collider is a trigger. OnTriggerEnter should work, but the character must be a CharacterController or Rigidbody (even a kinematic rigidbody). Check also the trigger size: it must be larger than the cube, or its collider will stop the character before the trigger detect it.

Checked both of these, yes the character is a kinematic rigidbody. The triggers are now much taller than the falling cubes (occupies space just above it). The cubes float away like in zero gravity, no wait time, no triggers fired.

Yes, it was deprecated because starting in Unity 5.6 you don't need an additional utility for component reordering. More information: https://forum.unity.com/threads/deprecated-xt-reorder-drag-drop-in-the-inspector.417572/#post-3090915

//UPDATE 2: FIXED! All working!

**IEnumerable instead of IEnumerator!!


using UnityEngine;
using System.Collections;

public class FallingCube : MonoBehaviour {
	
	// Use this for initialization
	void Start () {
		//Debug.Log("Start!", this);	
	}
	
	// Update is called once per frame
	void Update () {}
	
	void OnTriggerEnter(Collider other){
		//did the player's robot trigger it?
		if(other.transform.root.gameObject.name == "Robot00"){
			StartCoroutine(CubeFalling());
		}
	}
	
	IEnumerator CubeFalling(){
		yield return new WaitForSeconds(3.0f);
		//Debug.Log("wait", this);
		transform.parent.rigidbody.useGravity = true;
		transform.parent.rigidbody.isKinematic = false;
		//Debug.Log("done", this);
	}
}