- Home /
Best way to make a physics engine allow player to walk up stairs?
I have a physics engine that I want to allow the player to walk up stairs. right now, I have a raycast that sort of checks in front if there's a step and enough room to walk up it, but it looks choppy since the player technically "teleports" up stairs, even with a delay it looks strange. How did starbound/terraria do it? I considered ramps but it just didn't work.
If so, one thing you could do is treat the stairs as if they are a ramp - meaning the collision would be a ramp, but the graphics are stairs.
That way, you can just follow the line of the ramp which is a little bit easier.
as said above, i tried that, but i didnt like it. is there another way to do it?
Do you even walk up stairs in Terraria?
You get to the edge and if its below a certain height your character just moves up and across 1 pixel.
You've just got to Lerp the movement, thats all.
$$anonymous$$aybe not terraria. I just assumed since starbound is very similar. But what do you mean?
Answer by fuego_see_money · Jul 01, 2015 at 09:14 PM
Try this: note that the stairGroundY and stairTopY float values might have to be gotten differently depending on how you have your stairs set up. (This example assumes that the bottom of the staircase is the transform.position.y, not the top)
void OnTriggerStay2D(Collider2D c)
{
if(c.gameObject.tag == "Stairs")
{
Vector3 sPos = c.gameObject.transform.position;
float stairGroundY = sPos.y;
float stairTopY = sPos.y + c.bounds.size.y;
Vector3 pPos = transform.position;
float xPosRatio = (pPos.x - sPos.x) / c.bounds.size.x;
float yValueOnStairs = Mathf.Lerp(stairGroundY, stairTopY, xPosRatio);
transform.position = new Vector3(pPos.x, yValueOnStairs, pPos.z);
}
}
You can try that out - this is all from the top of my head so it may be flaky. Just comment if something doesnt work.
Thanks @fuego_see_money, and by the way you may want to check this question again http://answers.unity3d.com/questions/997372/child-object-consistency-issue.html
Your answer