Converve gravity action when overriding velocity

Hi everyone.

I have a GameObject with a Rigidbody attached. I'm moving it overriding directly its velocity, which works great. The Y velocity is conserved to work as expected. But with angled walls, the applied horizontal velocity forces it to climb the wall and gravity's action won't take any effect. With 90 angled walls it works fine.

Is there any alternative to move the object without having to change directly its Rigidbody's velocity?

Yes, use physics queries to determine if you can move rather than blindly changing the velocity. The solver has no idea about your custom behaviour because, well, it’s custom so you need to control that.

Thank you so much, @MelvMay . Could you provide an example, please?


Example of what specifically? How to use physics queries? That would be a tutorial really. ;)

My apologize, I failed to specify. Having this method that changes the velocity of a rigidbody and tries to maintain without any success the gravity effect over it inheriting the vertical velocity and while the body is moving against an angled wall, how could you conserve both forizontal and vertical velocity using physics queries?

private void SetBodyVelocity()
{
    float verticalVelocity = _playerRigidbody.velocity.y;
    Vector3 newVelocity = _playerTransform.forward * _velocity;
    newVelocity.y = verticalVelocity;
    _playerRigidbody.velocity = newVelocity;
}