How To Make My Player Only Move on the ground?
If the title was a little misunderstood, what I want is the player player to only be able to switch direction when on the ground, Like in real life. So they have to build up momentum in order to get to the next platform. I still want to have a bit of controllability in the air, but it's much less effective and the current momentum is affecting it. Here's the x-axis movement if you want to build off of it:
//X-axis imput
float x = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(x * playerSpeed, rb.velocity.y);
thanks!
Answer by yanerobot · Jun 02, 2021 at 12:32 PM
First of all, when you post your next question, add more relevant code. It’s pretty hard to explain you anything, when I don’t see what you’ve written.
Ok, basically, ou can’t use momentum based mechaincs when directly setting velocity of you character. When you say rb.velocity = -1 or 1 you are directly changing velocity of rigidbody. There are hundreds of ways to solve your problem, this is just of them: (Assuming your movement is happening in Move method and you have your “grounded” boolean)
void Move(float dir)
{
if (!grounded)
{
AirMovement(dir):
return;
}
//rest of your movement function
//...
}
void AirMovement(float dir)
{
rb.addForce(dir * _airSpeed, ForceMode.Force);
}
Notice: 1) if you call Move from an update method, you should multiply it by Time.deltaTime. 2) _airSpeed should be low. 3) your character in some cases can become too fast. You would want to cap his velocity somehow. Good luck.
Your answer
Follow this Question
Related Questions
How do I create a 2D movement script that will cause prefabs to drift around the screen? 0 Answers
How can I translate an object smoothly on the X Axis only? [C#] 1 Answer
Player Dips Into Ground After Jump 0 Answers
Snake movement from Snake VS Block 0 Answers
Forcibly halt movement of slow rigidbodies, but not falling ones. (2D) 1 Answer