- Home /
How do I use contacts in OnCollisionEnter?
Hi guys! I was just wondering; How do you use contacts? I refereed to some documentation (mainly http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html and http://docs.unity3d.com/Documentation/ScriptReference/Collision-contacts.html) and I have do not have the FULL idea how it's used and what it's used for. (I can only guess and assume some stuff)
And I was just wondering, is using contacts the way to go to check if it has collided vertically and horizontally? Because I want to do a game where the this character will turn back and walk the other direction if it hits the tile when it approaches on the left an right but if the character approached this tile from on top, the character will stop falling ans start walking.
I have used OnTriggerEnter before but it didnt work and so i moved top rigidbody.sweeptest but it was too messy. This is my current code.
// Actions
if ( TB_State == TetrisBabyState.Falling )
{
//if( rigidbody.SweepTest( Vector3.down, out hit, 5 ))
if( rigidbody.SweepTest( Vector3.down, out hit, Falling_Speed ) )
{
TB_State = TetrisBabyState.Walking;
}
else
{
Fall();
}
}
else if ( TB_State == TetrisBabyState.Walking )
{
// Check bottom, if has something
if( rigidbody.SweepTest( Vector3.down, out hit, Falling_Speed ) )
{
// Check right
if( Physics.Raycast( transform.position, Direction, out hit, 50 ) )
{
//print ("Tag:" + hit.collider.gameObject.tag );
if ( hit.collider.gameObject.tag == "EndPoint" )
{
DestroyTB();
}
else
{
// Check topright
if( Physics.Raycast( transform.position, DiagonalDirection, out hit, 100 ) )
{
// Check left
if( Physics.Raycast( transform.position, -Direction, out hit, 50 ) )
{
DiagonalDirection.x = DiagonalDirection.x * -1;
// Check topleft
if( Physics.Raycast( transform.position, DiagonalDirection, out hit, 100 ) )
{
// Change TetrisBaby into tile
ChangeTBToTile();
}
}
else
{
SwitchDirection();
}
}
else
{
TB_State = TetrisBabyState.Jumping;
}
}
}
else
{
Walk();
}
}
else
{
TB_State = TetrisBabyState.Falling;
}
}
else if ( TB_State == TetrisBabyState.Jumping )
{
//print ("JUMPED");
Jump();
}
Attached is also a picture of what i mean.