A little while ago I decided to convert my player movement script in my game from using a Character Controller to using a Rigidbody one since the former is so limited that it’s hard to add even basic features to it, as well as interacting more realistic with other rigidbodies in scene. But even after searching online how to do it, I had conflicting answers and they didn’t really work well.
The player now bounces everytime they move (using physics materials with bounce 0 doesn’t help), flies accross the map in specific circumstances, sometimes taking huge fall damage for no reason, can support itself on walls, and overall is a glitchy mess. Here is the parts of my script responsible for the player’s walking and jumping:
Falling (done before everything down here, inside the Update() method):
bool Grounded = Physics.CheckSphere (GroundCheck.position, Radius, GroundMask); //Checks if player is currently on the ground or airborne//
if (Grounded && PlayerRigidbody.velocity.y < 0f)
{
if (-PlayerRigidbody.velocity.y >= HarmfulVelocity)
{
float FallDamage = VelocityDamage * (-PlayerRigidbody.velocity.y / 10f);
Player.Hurt (FallDamage);
if (Player.NoiseLevel < FallingNoise)
{
Player.NoiseLevel = FallingNoise;
}
Debug.Log ("Player hits the ground with high speed;");
}
}
Main movement (done inside Update() method):
//Player main movement shenanigans//
float X = Input.GetAxisRaw ("Horizontal") * CurrentSpeed;
float Z = Input.GetAxisRaw ("Vertical") * CurrentSpeed;
Vector3 MovePosition = transform.right * X + transform.forward * Z;
GeneralVelocity = new Vector3 (MovePosition.x, PlayerRigidbody.velocity.y, MovePosition.z);
//Player main movement shenanigans//
Main movement 2 (done after that first part, inside fixedUpdate()):
void FixedUpdate()
{
PlayerRigidbody.velocity = GeneralVelocity;
}
Jumping (done inside Update() method, after main movement):
if (Input.GetKey(KeyCode.Space))
{
if (Grounded && CurrentStamina > MinStamina && PlayerStatus != PlayerState.Jumping && PlayerStatus != PlayerState.Falling)
{
Jump();
}
}
Jumping 2 (Jump() method, after all the lines shown above):
void Jump ()
{
CurrentStepLimit = JumpingStepLimit;
CurrentSlopeLimit = JumpingSlopeLimit;
if (Player.NoiseLevel < JumpingNoise)
{
Player.NoiseLevel = JumpingNoise;
}
PlayerRigidbody.AddForce(new Vector3(PlayerRigidbody.velocity.x, PlayerRigidbody.velocity.y + Mathf.Sqrt(JumpingHeight * -2f * Physics.gravity.y), PlayerRigidbody.velocity.z), ForceMode.Impulse); //Jumping MATH//
CurrentStamina += JumpingStaminaGain;
PlayerStatus = PlayerState.Jumping;
Debug.Log (this.name + " jumps;");
}
Here are my player’s rigidbody component parameters:
Imgur: The magic of the Internet
If you need more context, please tell me so I can reply with the whole script (yes I know people can copy it, but it is garbage anyway), it has functions like stamina, sprinting, crouching, etc, and they are all working fine. I have no idea what is wrong, none of the other functions inside the script mess with rigidbody so they shouldn’t have any effect here.
Also, the problem is definetely not rigidbodies’ themselves as I have used them in the game for other objects and they work just fine, with no issues at all.
