- Home /
Adding kinematic rigidbody makes OnTriggerExit break
Hello guys, I am with a problem with collisions at high speed that are driving me crazy. One of my attempts to fix it is to add a rigidbody component to all colliders in my player object. Formerly it has two triggers in child objects and one normal capsule collider at the root object used with character controller. One trigger has a kinematic rigidbody.
I am using a parenting system of moving plataforms, the problem is when I add a kinematic rigidbody to the other two (one collider and one trigger). The OnTriggerExit does not work with one script mine, but the OnTriggerEnter always work, can some one give me a light, I have no idea why this is occuring. The code used with OnTriggerEnter and exit is below, I think that the important part of code is only the trigger parts for this issue. Thank you.
#pragma strict
private var movingPlataform : GameObject = null; // The parent plataform
private var oldPosition : Vector3; // Last position
private var movingDirection : Vector3; // The direction where the plataform is moving
private var plataformerControl : PlatformerController = null;
// Player variables
private var playerLife : PlayerLife = null;
private var player : GameObject = null;
// The distance between the center of player and another wall to kill the player crushed
private var crushDetectDistance : float = 1.2;
// Set if this plataform can kill or not
var canKill : boolean = false;
function Awake ()
{
movingPlataform = gameObject.transform.parent.gameObject;
if( !movingPlataform )
{
Debug.LogError( "The pushing trigger can't find its parent." );
}
//Variables to detect crush
oldPosition = transform.position;
movingDirection = Vector3.zero;
}
function OnTriggerEnter (other : Collider)
{
var playerOb : GameObject = other.gameObject;
var platformerController : PlatformerController = other.gameObject.GetComponent( PlatformerController );
//If who collides is the player, make it a child,
//so the movement object and the player will share their locations
if( playerOb.GetComponent( PlatformerController ) )
{
playerOb.transform.parent = movingPlataform.transform;
// Getting player life
playerLife = other.gameObject.GetComponent( PlayerLife );
// Getting player object
player = other.gameObject;
plataformerControl = platformerController;
}
}
function OnTriggerExit (other : Collider)
{
var playerOb : GameObject = other.gameObject;
Debug.Log( other.gameObject.name + Time.time );
if( playerOb.GetComponent( PlatformerController ) )
{
plataformerControl = null;
playerLife = null;
playerOb.transform.parent = null;
player = null;
}
}
function Update ()
{
if( canKill && player != null )
{
var rayCastHit : RaycastHit;
// Getting the moviment direction
movingDirection = movingPlataform.transform.position - oldPosition;
movingDirection = movingDirection.normalized;
oldPosition = movingPlataform.transform.position;
// Getting the player position
var playerGlobalPosition = player.transform.position;
Debug.DrawLine ( playerGlobalPosition , playerGlobalPosition + movingDirection*crushDetectDistance, Color.cyan );
if( Physics.Raycast( playerGlobalPosition, movingDirection, rayCastHit, crushDetectDistance ) )
{
/*if( true || rayCastHit.transform.gameObject.tag == "ground" )*/
// The player can`t be crushed by the moving plataform itself or by triggers
if( rayCastHit.transform.gameObject != movingPlataform && !rayCastHit.collider.isTrigger )
{
playerLife.killPlayer();
Debug.DrawLine( playerGlobalPosition, rayCastHit.point, Color.magenta, 60 );
Debug.Log( rayCastHit.transform.gameObject.name );
}
}
}
}
I am having the same problem with OnColliderEnter with a collider! It works if is kinematic is off, but if its on the event doesn't fire. The only thing I can think of is because it is the child of another object, although the rigidbody and collider are on it (the child object).
@UnseenCream - you're describing expected behaviour. "collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached." - http://docs.unity3d.com/ScriptReference/Rigidbody.OnCollisionEnter.html
Indeed, if you need a rigidbody to be non-kinematic but behave like a kinematic you can use its Freeze-Constraints to hold it in place, with gravity off.
Your answer