Hello everyone, velocity y increases when my character runs on a sloped ground, probably due to slippage, (by the way i don’t fully understand this, I’d appreciate it if you could point it out.) That sloped ground consists of a box collider with the z-axis turned. When I accelerate my character only to the right or left, it goes uphill quickly, but I don’t want the y velocity to increase.
On such slopes, the character jumps by himself because x-force of the RB velocity is resetting but y-force is free in sudden stops. I hope I was clear enough, thanks!
Not 100% sure what you’re speaking of, but I’ll guess;
If you are on a slope and force your character to the right at 1 unit per second, a rising slope would force your character to rise enough to not be colliding, correct? And hence the character would be going above 1 unit per second total velocity, diagonally up the slope.
Do you perhaps intend to tilt the motion of your character according to the slope of the ground he is standing on?
This can be done by raycasting down to hit the ground and using the normal of that contact point to tilt the player’s input before applying it. This would allow you to keep constant speed on any slope.
If that’s not it, I have no idea what you mean.
Thanks! that’s exactly what i said, better to use collider surface’s normal, so that rb y-velocity will also be multiplied by horizontal input while on sloped ground, so there will be no jumping when the character stops. Lastly, is there a more sensible option other than freezing the x position or increasing the friction value of the physic material so that the character doesn’t slide down the slope ?
“Sensible” isn’t really a useful metric.
Each approach would have different implications.
-
freezing the x position: now other things can’t just knock you away
-
increasing friction: you could be knocked away by a large physics hit, for example
Or maybe you don’t have anything moving in game that might hit you.
But every engineering decision will fundamentally alter the play mechanics. Quantify those mechanics and make your game design decisions!
I understand, thanks for the answer, I use the “ground hit normal” and it worked. Character no longer jumps on its own when stopped.
if (OnGround)
{
Vector2 velocity = new Vector2(m_LastTouchedGroundNormal.y, -m_LastTouchedGroundNormal.x) * horizontal * speed;
m_Rigidbody.velocity = velocity;
Debug.Log(m_LastTouchedGroundNormal);
Debug.Log(velocity);
}
else
{
m_Rigidbody.velocity = new Vector2(horizontal * speed, m_Rigidbody.velocity.y);
}
if (horizontal != 0)
{
// m_Rigidbody.velocity = new Vector2(horizontal * speed, m_Rigidbody.velocity.y);
if ((m_Rigidbody.constraints & RigidbodyConstraints2D.FreezePositionX) == RigidbodyConstraints2D.FreezePositionX)
{
// m_Rigidbody.constraints &= ~RigidbodyConstraints2D.FreezePositionX;
StartCoroutine(SetCharacterRBConstraints(RigidbodyConstraints2D.FreezePositionX, false));
}
}
else if (!OnWall)
{
if ((m_Rigidbody.constraints & RigidbodyConstraints2D.FreezePositionX) != RigidbodyConstraints2D.FreezePositionX)
{
//m_Rigidbody.constraints |= RigidbodyConstraints2D.FreezePositionX;
StartCoroutine(SetCharacterRBConstraints(RigidbodyConstraints2D.FreezePositionX, true));
}
}