5
\$\begingroup\$

When I go down slopes with character controller, if the speed is too high, the character just flies off the slope and lands at the bottom. I want my character to "stick" to the slope rather then just jump of it. I came up with code to do so, but it didn't turn out as I planned. Now the character just slides on the slope when I stop pressing the arrows (not staying at its place on the slope).

Physics.Raycast (transform.position, Vector3.down, out hit);
transform.position = transform.position
                     + new Vector3(0f, distance - hit.distance, 0f);

Thanks for the help, i couldn't figure this out all day.

\$\endgroup\$
  • \$\begingroup\$ What if you set a limit to his velocity \$\endgroup\$ – Artsicle Jan 14 '17 at 11:20
1
\$\begingroup\$

I know a few titles that have an "on the ground" mode that enforces some stickiness to handle problems like this. When the object is sticky the usual lateral forces work as expected, but the vertical (gravity) force is ignored and heights are forced to ground.

Part of this mode has enough smarts to unstick the object appropriately, for example when a jump is activated or some external impulse like getting hit activates a ragdoll.

In real life movement if you were running towards a 45deg ramp you too would go flying into the air. That's just physics. You can't really hack that with greater gravity because... why not just lock the object down with sticky in the first place if you're going to hack.

I believe that stopping this behavior artificially is warranted here to make gameplay more solid.

\$\endgroup\$
0
\$\begingroup\$

Try something like this, i haven't tested it yet, but i can't see why it wouldn't work

Physics.Raycast (transform.position, Vector3.down, out hit);

Vector3 temp = transform.position;
temp.y = hit.point.y + hoverDistance;
transform.position = temp;
\$\endgroup\$
0
\$\begingroup\$

Try increasing gravity dramatically so that there is a force keeping the player on ground.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.