- Home /
Objects continue movement despite being triggered to stop
Basically there are boxes falling from the top of the screen that keep stacking up on each other. I'm using a BoxCollider2D
trigger to detect when these objects collide and then stop their movement in the Update() function.
void Update () {
if(fall)
transform.Translate (Vector3.down * Time.deltaTime * fallSpeed, Space.World);
}
void OnTriggerEnter2D(Collider2D col){
Debug.Log ("TriggerEnter");
fall = false;
}
void OnTriggerExit2D(Collider2D col){
Debug.Log ("TriggerExit");
fall = true;
}
But sometimes these objects don't stop exactly when the trigger is detected. Even worse, there are times when the objects stop before colliding with each other. I haven't used physics because it causes a reverse reaction and the boxes don't stack up properly, they tend to slip and form irregular stacks.
What can I do to make sure the objects stop exactly when the trigger is detected? I am new to Unity so I'm sorry if this question seems stupid to most of you experts out there.
Here's a crude little snippet from a really old script I used. Dissect it and I think you'll see what it does.
function OnCollisionEnter(Land : Collision)
{
if(Land.collider.gameObject.name == "InnerCylinder")
{
constBlockAudioSource.Play();
//audio.PlayClipAtPoint(audioClip, Vector3 (0, 0, 0));
rigidCVB.is$$anonymous$$inematic = true;
transform.parent = Land.collider.gameObject.transform;
}
else if (Land.collider.gameObject.name != "InnerCylinder" && Land.collider.gameObject.name != "OuterEdge")
{
var kinOb : GameObject = Land.gameObject;
var isIt$$anonymous$$in : boolean = kinOb.GetComponent.<Rigidbody>().is$$anonymous$$inematic;
if (isIt$$anonymous$$in == true)
{
rigidCVB.is$$anonymous$$inematic = true;
if(autoPullIn == true)
{
transform.parent = Land.collider.gameObject.transform;
}
}
else
{
if( !rigidCVB.is$$anonymous$$inematic )
{
GetComponent.<Rigidbody>().velocity = Vector3(0,0,0);
}
}
}
}
It was a tetris-esque game in 3D.