How do you stop counter movement while in the air?
Example of the bug I'm having: https://youtu.be/w6tAEo2d6UI
I'm having a bug in my project that I've tried so long to figure out. My character movement script is based off the script Dani made on his tutorial channel: https://www.youtube.com/watch?v=XAC8U9-dTZU.
I've added sticking and leaping to the script. My big problem is when I leap:
void Leap()
{
float leapThisFrame = leapAmount;
rb.AddForce(LeapDirection.transform.forward * leapThisFrame);
}
I find the counter movement function in my script and as you can see if the player is not grounded or is jumping counter movement should be disabled.
private void CounterMovement(float x, float y, Vector2 mag)
{
if (!grounded || jumping) return;
//Slow down sliding
if (crouching)
{
rb.AddForce(moveSpeed * Time.deltaTime * -rb.velocity.normalized * slideCounterMovement);
return;
}
//Counter movement
if (Math.Abs(mag.x) > threshold && Math.Abs(x) < 0.05f || (mag.x < -threshold && x > 0) || (mag.x >
threshold && x < 0))
{
rb.AddForce(moveSpeed * orientation.transform.right * Time.deltaTime * -mag.x *
counterMovement);
}
if (Math.Abs(mag.y) > threshold && Math.Abs(y) < 0.05f || (mag.y < -threshold && y > 0) || (mag.y >
threshold && y < 0))
{
rb.AddForce(moveSpeed * orientation.transform.forward * Time.deltaTime * -mag.y *
counterMovement);
}
//Limit diagonal running. This will also cause a full stop if sliding fast and un-crouching, so
not optimal.
if (Mathf.Sqrt((Mathf.Pow(rb.velocity.x, 2) + Mathf.Pow(rb.velocity.z, 2))) > maxSpeed)
{
float fallspeed = rb.velocity.y;
Vector3 n = rb.velocity.normalized * maxSpeed;
rb.velocity = new Vector3(n.x, fallspeed, n.z);
}
But it seems that there is a very slight delay and when I try to leap my character starts leaping then somehow touches the ground again before going into the air and just stops the leap mid air. I've tried to transform the player up a little then leap but it still is so fast that the script still thinks the character is grounded. I can get around that by putting in an invoke but then the leap is delayed and it feels very choppy. I have also tried to set grounded = false in the leap function and that does nothing. You can see in the video on the right the bool for leaping is always set to false even if the character is leaping. I can also delete counter movement in the script and just use unity's friction but that makes it so I can't go up slopes or slide.
I am just trying to see what I can do to make it so there is not counter movement when I'm leaping! Definitely if you guys have any ideas to try let me know.
Answer by antounk · Dec 30, 2020 at 08:55 AM
hello, from what i can tell the player becomes grounded after he has counter movement applied; it's a timing error i think so:
try putting the ground check function before you're applying movement
since you're using dani's character controller, for that to work, instead of using OnCollisionStay to check if your player is grounded, do a normal ground check:
void handleGrounded()
{
if (Physics.CheckSphere(groundCheck.position, GroundDistance, whatIsGround))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
}
and then to check before applying movement:
void Update() // can be FixedUpdate
{
handleGrounded();
Movement();
}
hope that helped, and goodluck in the game you are working on
oh btw, GroundDistance is a float, and groundCheck is a transform close to the player's feet