Search Unity

I character gets launched into the air

Discussion in 'Scripting' started by Robster95, Jul 8, 2021.

  1. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    I have a rigidbody third person character that the player moves around with the left joystick on a controller on a terrain with a bunch of small hills and dunes. The movement is determined by the value of the joystick on the x or y value then multiplied by a speed variable. The player can dash while moving the character which will take the current movement direction and multiply the speed variable then lerp it back to the normal speed.

    Sometimes when I make the player dash they fly into the air. I think it's because of the terrain and the character might be clipping into the ground then pushed out but I'm not sure.

    I'm using a normal ground check system that casts a spherecheck downwards and if it hits something than it registers that it's grounded but the script also has a slope check that shoots a raycast down (slightly farther than the spherecheck) and if that registers instead than it is on a slope.

    If anyone has any idea on what I can do to possibly fix this bug please let me know!!
    Thank you
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    This is a good theory. Go prove it... every frame print out the
    rigidbody.velocity.y
    of your character. I haven't done dashing with a Rigidbody... it might be necessary to prevent the character from accelerating vertically, and here are few possible ways:

    - transiently increase gravity during the dash

    - manually apply extra gravity during the dash (add downward velocity)

    - watch the velocity each frame and compare it to the previous, and if it changes upwards "too much" (whatever that means), return it to its pre-dash value.

    Remember, just like with all Vector3 properties, to change just one value (such as .y), you need to read the entire Vector3 velocity out, change the .y, then put it back:

    Code (csharp):
    1. Vector3 velocity = myRigidbody.velocity;
    2. velocity.y = 0;
    3. myRigidbody.velocity = velocity;
     
    Robster95 likes this.
  3. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    Ok I'll try this right now. The dash should only be changing the x and the z of the velocity not the y so this will be interesting to see. Also I feel as though if I apply more gravity it would make the character feel heavier and not dash as far. Maybe I can make a coroutine change the physics material to a more slippery one than change it back to it's normal physicals material after it's done dashing?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    That might have even more knock on effects... try that code snippet above during dash... or even instead of setting .y to zero add EXTRA gravity downard each frame.
     
  5. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    Just tried it and the player was launched into the air again but the y on the movement velocity did not change. It was set to 0
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Is your dash vector pointing slightly upward? Are you multiplying by the player's transform / rotation, like maybe he's leaning backwards a little so his
    transform.forward
    actually has an up component during the dash?
     
    Robster95 likes this.
  7. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    That's not a bad idea, I didn't even think that could affect things. I looked at it right now and the player doesn't change their rotation. The player has a cinemachine following the body and that rotates with the right joystick but the players body itself does not
     
  8. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    As for the dash. It only affects the x and z on the movement vector so it wasn't point up or down just perpendicular the body of the player