- Home /
Rigidbody controller counter movement
Basically I'm doing a FPS controller with a rigidbody, since I want the movement to have physics involved. But I'm kind of a noob and I am not able to add some counter movement so when the player is on the ground, they can controll themselves better and not slide around everywhere. This is what I currently have: #region Variables //Camera public CameraMovement camera;
//Movement
public int speed = 20;
public int maxSpeed = 20;
public int slowDownFactor = 10;
private float hMovement;
private float vMovement;
private Vector2 movement;
private Vector2 prevMovement;
private bool jumping;
//Jumping
public int jumpForce = 10;
public int extraGravity = 10;
//Player
private Rigidbody player;
private CapsuleCollider collider;
#endregion
#region Unity Callbacks
// Start is called before the first frame update
void Start()
{
player = GetComponent<Rigidbody>();
collider = GetComponent<CapsuleCollider>();
}
// Update is called once per frame
void FixedUpdate()
{
Movement();
//CounterMovement();
Rotate();
if (Input.GetKeyDown(KeyCode.Q)) Debug.Log(Grounded());
}
#endregion
#region My methods
//The player movement
private void Movement()
{
//Gets the input
hMovement = Input.GetAxisRaw("Horizontal");
vMovement = Input.GetAxisRaw("Vertical");
movement = new Vector2(hMovement, vMovement);
//Jumping
jumping = Input.GetButton("Jump");
//If the movement is more than the maximum, cancel out the input(x)
if (player.velocity.x > maxSpeed && hMovement > 0) hMovement = 0;
if (player.velocity.x < -maxSpeed && hMovement < 0) hMovement = 0;
//If the movement is more than the maximum, cancel out the input(z)
if (player.velocity.z > maxSpeed && vMovement > 0) vMovement = 0;
if (player.velocity.z < -maxSpeed && vMovement < 0) vMovement = 0;
//Jumping
if (jumping && Grounded())
{
player.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse);
}
//Adds the force to the rigidbody
player.AddForce(hMovement * transform.right * Time.deltaTime * speed, ForceMode.Impulse);
player.AddForce(vMovement * transform.forward * Time.deltaTime * speed, ForceMode.Impulse);
//Extra gravity
player.AddForce(new Vector3(0, -extraGravity, 0));
}
//The counter movement, to avoid sloppy movement
private void CounterMovement()
{
Vector3 counterMovement = new Vector3(-player.velocity.x, 0, -player.velocity.z);
if ((prevMovement.x != movement.x || prevMovement.y != movement.y) && Grounded())
{
//Add some counter movement if the player has stopped pressing a movement key, or pressed a new one
player.AddForce(counterMovement * 40);
prevMovement = movement;
}
}
//Rotates the player on the y with the mouse
private void Rotate()
{
transform.rotation = camera.playerRotation;
}
//Determins if the player is grounded
private bool Grounded()
{
return Physics.Raycast(transform.position, Vector3.down, collider.bounds.extents.y + .1f);
}
#endregion
I've been stuck on this for a long time, so I finally decided to ask for help. Thanks in advance.
Answer by alwayscodeangry · Feb 22, 2020 at 05:49 PM
Have you tried adding a PhysicsMaterial to your ground surface collider? That should allow you to increase the surface friction values which would hopefully achieve the effect you're describing. It would also play nice with the physics engine which is always a nice bonus :)
Edit: My other suggestion would be to set the rigidbody's linear drag to a suitably high value when you want to slow it down rapidly:
if ((prevMovement.x != movement.x || prevMovement.y != movement.y) && Grounded())
{
player.drag = COUNTER_DRAG;
}
else
{
player.drag = DEFAULT_DRAG; // probably 0.0f, but it's always good to avoid those hard coded constants...
}
That's what I initially thought to do but the thing is, it slows down the overall movement, so I have it on very low friction. What I want is the player to be able to change directions and come to a stop rapidly, because right now the movement is too heavy and uncontrollable.
I've added a second option, hopefully that works for you :)
Your answer
Follow this Question
Related Questions
Rigidbody FPS controller? 0 Answers
How do I convert my FPS controller movement script to a rigidbody to prevent clipping. 0 Answers
How can I get a responsive rigidbody FPS controller without acceleration that reacts to forces? 2 Answers
My Rigidbody FPS controller is falling very slow, please help! 2 Answers