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.
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.
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.
– MidnightDemonIf the question is answered, mark it as such.
– BlankBlank5454I can't figure out how...
– MidnightDemonClick on the check mark.
– Eric5h5