How to stop cube from sliding (rg2d.velocity)
Hi all,
I'm currently busy with my charachter controller for my 2D game and like the smoothness from 'velocity' however i can't seem to solve the slipping from the charachter. Is there a solution for this problem?
My code:
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
if (moveH > 0)
{
rb2D.velocity = new Vector2(moveH * maxSpeed, rb2D.velocity.y);
}
else if (moveH < 0)
{
rb2D.velocity = new Vector2(moveH * maxSpeed, rb2D.velocity.y);
}
else
{
rb2D.velocity = new Vector2(0, rb2D.velocity.y);
}
if (moveV > 0)
{
rb2D.velocity = new Vector2(rb2D.velocity.x, moveV * maxSpeed);
}
else if (moveV < 0)
{
rb2D.velocity = new Vector2(rb2D.velocity.x, moveV * maxSpeed);
}
else
{
rb2D.velocity = new Vector2(rb2D.velocity.x, 0);
}
Debug.Log(moveH);
Debug.Log(moveV);
}
Kind regards,
Ruben
Answer by Ran_Crump · May 27, 2019 at 05:06 PM
A quick and simple way would be to turn on Gravity Scale which will pull the object down to the colliders and slow it down due to collision. Another would be to create a Physics 2D Material and assign it in the Material Slot of the Rigid-body component in it you can control the amount of friction and bounciness the user experiences.
Hi @Ran_Crump, Thanks for your reply!
I'm creating a binding of isaac/enter the gungeon kind of game so the player won't really have physics as it needs to be able to walk up, down, left and right. I also created the 2d material for the rigid body but that doesn't seem to work either - will try some other things but friction doesn't seem to work as of now.
~Ruben
(The last 1/0.5 seconds is when I released the input key, then the velocity is going down however I want it to stop immediately)
Be sure you're applying the material to the rigidbody character and the physics material slot on the collider of the terrain
I tried that but that sadly doesn't work. If I'm adding a collider on the 'background'/cube behind the player, the player/cube will glitch (because the player has a colider as well)