- Home /
Preserve horizontal speed on slopes
Hey guys I have a 3D character controller with rigidbody that moves along a grid. Basically, if you swipe right, the character will move right until he hits a wall. I also have gravity so my player can jump.
The issue is, when my player climbs a slope or stairs, he slows down drastically, because gravity is pulling him down. Is there a way to preserve my horizontal speed even on upward slopes?
Here is my movement code
//If we are not moving...
if (!isMoving)
{
//If requested direction is valid, set it to currentDir and rotate character
if (CheckDirection(nextDirection))
{
currentDirection = nextDirection;
RotateCharacter(currentDirection);
}
//If we are stopped, calculate the next valid start and end position
startPosition = rb.position;
endPosition = startPosition + (currentDirection * unitLength);
//Round the result so we end up on a whole number coordinate
endPosition = new Vector3(Mathf.Round(endPosition.x), Mathf.Round(endPosition.y), Mathf.Round(endPosition.z));
isMoving = true;
}
//If we are moving and the current direction is valid (no walls in the way)
if (isMoving && CheckDirection(currentDirection))
{
//Move rigidbody towards destination
float step = speed * Time.deltaTime;
rb.MovePosition(Vector3.MoveTowards(rb.position, new Vector3(endPosition.x, rb.position.y, endPosition.z), step));
}
else
{
RotateCharacter(nextDirection);
}
//Calculate horizontal distance remaining from startpoint to endpoint
Vector3 distanceRemaining = new Vector3(endPosition.x, 0, endPosition.z) - new Vector3(rb.position.x, 0, rb.position.z);
//If we are extremely close to endpoint, stop
if (distanceRemaining.sqrMagnitude < float.Epsilon)
{
isMoving = false;
}
//If we hit a wall, stop
if (!CheckDirection(currentDirection))
{
isMoving = false;
}
Answer by WarmedxMints · Feb 28, 2019 at 07:19 PM
A few things which come to mind are do away with Unity's physics and use a raycast system for collisions which applies gravity in the players transform direction. Change the vector of gravity to your players transform.down direction using Physics.gravity which would mean you player may walk on walls though or modify the force when traversing a slope.
Your answer
![](https://koobas.hobune.stream/wayback/20220612192658im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Ramp for stairs causes player to slide down. 1 Answer
Why reducing material friction doesn't make static rigidbody move with gravity 1 Answer
Instantiated prefab changes gravitational speed when dragged? 0 Answers
Raycasting ray go through object with gravity enabled 0 Answers
Turret floats away and pushes everything away from it 2 Answers