Hello guys, after searching in the web about moving plataforms, I choose to implement a moving plataform using parenting triggers. The triggers are in the sides of the plataform, and when you enter in it you become parented to this, moving together with the plataform.
It works well if the plataform isn`t super fast. But my problem is, how can I check with the plataform crushed my player against some wall using a box collider.
Actually when it crushes the player, it only push the character inside the box collider. I was thinking to check in script something like:
Well, after research for some time, I did one script that works with a raycast. But it has some problems, can some1 help me:
The player can pass through the ground if the ground is going in the direction of the player and the player is in a high velocity;
The player scale glitches sometime when leaving the moving plataform;
The moving plataform has a limit speed to works well;
Code here, based on parenting moving plataform:
#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;
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 );
}
}
}
}