A good way to do this would be to collide if Vector3.Dot (or Vector2.Dot) of Player.velocity && rayInfo.normal (for a simple raycast) is less than 0. In order for this to work you need to go into Edit-->Project Settings-->Physics and turn on the toggle for "raycast hits triggers."
var Trans : Transform;
//or alternatively use:
private var Trans : Transform;
Trans = transform; //will not work with #pragma strict
function FixedUpdate ()
{
var rayInfo : RaycastHit;
var halfHeight : float = 1; //One half the player's height
if(Physics.Raycast(Trans.position + (0.05 - halfHeight)*Vector2.up, -Vector2.up, rayInfo, 0.05)
{
Collide(Vector2(0,0)); //collide with a motionless platform if the dot is zero
//INSERT Disable gravity code here
}
}
My collide code will help. It takes in a platform relative velocity variable, so if you want moving platforms, you can.
You can not just plug in these functions and see everything magically work. Personally, I think you should work to understand this code. I presume you are working in 2D, if not then should read up on 3D dot products.
Learn Dot Products, they essentially check if two vectors (such as velocity vectors and normal vectors (in this case) are facing in the same direction). For two normalized vectors, if the dot product is 1, they are parallel (0 degrees), dot product of -1 means the vectors are going away from each other (180 degrees)[which is what we are checking for], 0 means perpendicular (90 degrees or 270 degrees)
function Collide (relVel : Vector2)
{
Cross(controller.perceivedNormal);
if(Vector2.Dot(controller.perceivedNormal,Vector3(controller.velocity.x - relVel.x,controller.velocity.y -relVel.y,0)) < 0)
{
controller.velocity = relVel + Vector3.Project(Vector3(controller.velocity.x - relVel.x,controller.velocity.y - relVel.y,0),Vector3(controller.moveDir.x,controller.moveDir.y,0));
}
}
function Cross () //this is a Cross Product that is optimized for 2D
//it finds "rightward" and thus might be irrelevant in your game
//if platforms are not slanted
{
controller.moveDir = Vector2(normal.y,-normal.x);
}
This is a makeshift post which has not been catered to the variables in the OP, so you will have to change the functions a little (who knows maybe a lot).
↧