Hello,
I’m trying to make a gameplay where you can jump between asteroids in a 0 gravity space. I want my player to stick to the asteroid and be able to walk along its surface until he jump to go to another asteroid.
I was able to create something where I check surface and if the feet are grounded, I apply a force from the player toward the surface, but on sharp edges or if i go too fast, my check may fail and my player is propulsed away.
I need to be scratched until I decided not to.
Relevant part of my current code:
private void CheckGrounded()
{
// Perform a raycast to check for ground, ignoring trigger colliders
RaycastHit2D hit = Physics2D.Raycast(feetPosition.position, -transform.up, feetDistanceCheck, groundLayer);
if (hit)
{
// Align the player's rotation with the surface normal
surfaceNormal = hit.normal;
float angle = Mathf.Atan2(surfaceNormal.y, surfaceNormal.x) * Mathf.Rad2Deg - 90f;
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, angle), 0.1f);
rb.freezeRotation = true;
isGrounded = true;
// Apply surface attachment if not jumping
if (jumpTimer <= 0)
ApplySurfaceAttachment();
}
else
{
isGrounded = false;
rb.freezeRotation = false;
}
}
private void ApplySurfaceAttachment()
{
Vector2 force = surfaceNormal * -surfaceTension;
rb.AddForce(force);
}
There is one actual good behavior to keep: you can choose to go slowly to a place, stop, jump straight. Or speedrun by walking fast and jump to keep the momentum while unscratching the surface.
Do you have any information on how to achieve this?