I made a simple jumping mechanic using a raycast to check for “grounded” status.
However, when the character lands after jumping, sometimes it will stop a little higher, sometimes a little lower than expected.
Maybe I could use the Y coordinates of the platform to make it stop precisely where it’s meant to, but I have no idea how to do that. I’m really inexperienced.
Here’s my code:
void FixedUpdate ()
{
Raycasting ();
if (spaceisdown == true)
{
y_veloc = y_velocmax;
spaceisdown = false;
}
if (grounded == false)
{
y_veloc = y_veloc - gravity;
}
transform.Translate (Vector2.up * y_veloc * Time.deltaTime);
}
void Raycasting()
{
Debug.DrawLine (this.transform.position, groundedEnd.position, Color.green);
grounded = Physics2D.Linecast (this.transform.position, groundedEnd.position, 1 << LayerMask.NameToLayer ("ground"));
}