- Home /
"Jumping" Mechanics
I'm trying to write the most simple jumping mechanics that I possibly can for a 2D sidescroller. Here's what I have:
var can_jump : boolean;
var velocity : float;
var jump_delay : float;
var jump_reset : float;
var passed_in_ok : boolean;
var jumping : boolean;
var test_hit : GameObject;
function Start () {
}
function Update () {
var ray = new Ray(transform.position,Vector3(0,-1,0));
var hit : RaycastHit;
if (collider.Raycast (ray, hit, 100.0))
{
Debug.DrawLine (ray.origin, hit.point);
Debug.Log("Something!");
hit.transform.gameObject.renderer.material.color = Color.red;
}
if(rigidbody.velocity.y == 0 || passed_in_ok == true)
{
can_jump = true;
}
else
{
can_jump = false;
}
if(can_jump == true)
{
if(Input.GetKey(KeyCode.Space))
{
jump_delay = jump_reset;
rigidbody.velocity = Vector3(0,velocity,0);
}
}
}
The raycast part does NOT work at all. What I was trying to do there was tell if there was a surface immediately beneath the object, then highlight it red to indicate that it can tell that it's there. Well that doesn't work at all, also, just having it only allow you to jump when the velocity is at 0 results in very sluggish jumping where the object will take a second to jump again once it's hit the ground. The closest I got to "good", workable mechanics was having thin, invisible colliders on the platforms that can detect when the object is there, and then it will allow the object to "jump" regardless of current velocity, but this seems inefficient to have to put an invisible collider on every object and use "OnTriggerEnter" all the time. So I'm just trying to determine now if there's a way to tell if there's a surface directly below the object or tell if it's on the "ground" in a way.
Is this possible? If so, how does one accomplish this?
Your answer