- Home /
How to move along slopes with a Rigidbody2D?
I have been trying for a while to find out how to have the player move along a slope with a 2D rigidbody. How I attempted to do it is by raycasting from the bottom of my player's capsule collider, getting the angle of that, and multiplying that to my velocity.
private void SlopeCheck() // called from Update()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position - new Vector3(0, bc.size.y / 1.6f, 0), Vector2.down, groundCheckDistance, ground);
if (hit)
{
slopePerpendicular = Vector2.Perpendicular(hit.normal).normalized;
Debug.DrawRay(hit.point, hit.normal, Color.red);
Debug.DrawRay(hit.point, slopePerpendicular, Color.blue);
}
}
private void Movement() // called from FixedUpdate()
{
if (isGrounded && !isJumping)
{
rb.velocity = new Vector2(moveInput * speed * -slopePerpendicular.x * Time.fixedDeltaTime, moveInput * speed * -slopePerpendicular.y * Time.fixedDeltaTime);
}
else
{
rb.velocity = new Vector2(moveInput * speed * Time.fixedDeltaTime, rb.velocity.y);
}
}
I have two problems with this: It only detects the slope from that one hit. I'd prefer it to raycast from the point of my capsule collider that collides with the ground.
If I'm moving too fast, it skips the check when transitioning slopes and I get launched up the slope. Maybe I could raycast where I will be instead of where I am?
I have no idea how to solve these problems (should I just not bother and try using custom 2D physics?), so any help would be greatly appreciated.
Answer by highpockets · May 04, 2020 at 09:41 PM
What about working with contact points from collisions. You can get the contact point normal on collision enter, so each time you enter a new grade of a slope or flat ground, you can test to find the normal. https://docs.unity3d.com/ScriptReference/ContactPoint.html
By the way, you would have to filter out a lot of the collisions, but if you just make sure that the collision is in the right spot in relation to the direction your moving, this should work
Answer by TCMOREIRA · May 05, 2020 at 12:09 AM
Have you tried utilizing a Circle Collider whenever touching slopes?
Some games utilize different colliders attached to Game Objects depending on the current context.
So you can simply listen for the collisions that matter and ignore the other ones (or activate and deactivate the colliders accordingly).
A circle collider is great for slopes for it's ability to roll up and down, given the correct amount of force and friction onto the rigidbody.
I have tried that, and it does work, but only if I'm moving slow. If I'm moving fast, I start to bounce when moving along a slope (this is most apparent with steep slopes).
Answer by Cameroniy · May 05, 2020 at 10:49 AM
Right now, the character can't even stand still on a slope without sliding down. I know if I set the character's friction higher, they will be able to stand still on the slope. However turning up the character's friction affect's negatively affects character's movement mcdvoice.com
I think you can fix that by setting the friction higher only if you are still on a slope, or by setting gravity to zero when grounded and not moving.
Answer by Kylelove · Aug 12, 2020 at 08:06 PM
To access all of above said is very easy to do the task given as per the requirement. https://iflyswaguide.online/