Stick to ground

Hello, I have an issue where my character should be sticking to the ground, and, while he does, it’s like there’s some “magnet force” or something that happens when running though slopes.

If I run on a convex surface, my character is no longer “touching” the ground as if he was pulled away from the surface but held back by an elastic rope, even though he’s still considered “on ground”.

If I run on a concave surface, then he gets a bit into the ground.

The faster I go or the harder the slope, the bigger the effect.

Here’s how it looks like, look when he enters the concave part, and then the convex part :

Here time scale equaled 0.5 :

Here, it equaled 1 :

Also, the values for the groundHit indeed changes instead of always staying the same (the value I’m aiming for is 0.25, and here it can go up to 1.5 and down to 0.05 for example).

Here’s the code for the OnGround method :

  public void OnGround() {
    float maxLength = 0.5f;
    if (onGround) maxLength *= Mathf.Clamp(RB.velocity.magnitude / 10f, 1f, 5f);

    if (Physics.Raycast(transform.position, -transform.up, out groundHit, maxLength, collisionMask)) {
      Debug.Log("Is on ground");
      Debug.Log("hit dist: " + groundHit.distance);
      RB.position = groundHit.point + transform.up * 0.25f;
      onGround = true;
      return;
    }
    Debug.Log("Is NOT on ground");
    onGround = false;
  }

Thanks for the help !

Other example : Screen capture - 81ee266ddcf08e00e790621bae657128 - Gyazo

He should not get “off the ground” visually (even though he’s still considered “on ground”)

What happens if you’re moving slowly? What happens if you’re standing at the top of those hills in your videos? Is it only while in motion?

Also, when do you call this OnGround method? Is that the only code you rely on to place it on the ground? You must have other code that actually moves the capsule… what does that do?