# Bouncing while moving along the ground, what is causing this?

So we are using a rigidbody and a character controller we wrote that is just moving the player to the left and right but as we move left and right the player is hopping every so often randomly. Does anyone have any ideas what is causing this and or how to fix it?

Are you sure you aren’t hitting any colliders?

Shouldn’t be. I am just moving along a flat platform…

Having both rigidbody collider and character controller in one gameobject is big no no. You need to either use rigidbody or character controller,not both.

We don’t have a rigidbody and a character controller object on the same object. I meant we wrote our own character controller code, IE what moves the character around.

Aha I see. Well then I would guess its propably something wrong with code If you can paste it here I can try to help.

``````public class TWCharacterController : MonoBehaviour{
// horizontal movement variables.
private float		baseSpeed = 5.9F, speed, maxSpeed = 6;
private Vector3		horizontalDirection = Vector3.zero;

// jumping variable.

public float		jumpSpeed = 250.0F, airControl = 0.5F;
private Vector3		verticalDirection = Vector3.zero;
private bool		isGrounded = true, jumping = false, inAir = false,
jumpClimax = false, platformCollision = false;

// gravity variables
public float		forceOfGravity = 10.0F, fallSpeed = 2.0F;
private Vector3		gravity = new Vector3(0, 10.0F, 0);

// storing the script that is attached to the collider on the bottom of the player.
PlayerHitBottom playerhitbottom;

// function to move the character horizontally.
void Movement(){
// if the player is jumping or is falling in the air set is grounded to false and reduce their horizontal speed.
if(jumping || inAir){
isGrounded = false;
speed = baseSpeed*0.8F;
}
// if they arent in the air or jumping set speed back to normal.
else{ speed = baseSpeed; }

// check for horizontal movement.
horizontalMoveKeyboard();

// check for jump
verticalMove();

// apply gravity
if(!platformCollision){ applyGravity(); }
}

// handle the vertical jump
void verticalMove(){
//checks to see if the player pressed the jump button and the player is on a platform, ie touching the ground.
if(Input.GetButtonDown("Jump")  platformCollision){
Jump ();
}
}

void Jump(){
if(jumping){ return; }
// set jumping to true and platformcollision to false. Basically saying we arent on the platform and we are going up in the jump.
jumping = true;
platformCollision = false;
// add the jump force that propells the player upward.
}

void horizontalMoveKeyboard(){
float horizontal = Input.GetAxisRaw("Horizontal");

horizontalMove((int)horizontal);
}

// Moves the player horizontally
void horizontalMove(int horizontal){
if(horizontal == 0){
horizontalDirection = -rigidbody.velocity;
}
else{
// get the horizontal axis relative to the player and the correct direction.
horizontalDirection = this.rigidbody.transform.right * horizontal * speed * this.transform.localScale.x;
}

// if we are rotated handle the movement direction correctly.
if(this.transform.rotation.z == 0 || this.transform.rotation.z == 1){
if(rigidbody.velocity.x > maxSpeed){
rigidbody.velocity = new Vector3(horizontal * maxSpeed, rigidbody.velocity.y, 0);
}
}
else{
if(rigidbody.velocity.y > maxSpeed){
rigidbody.velocity = new Vector3(rigidbody.velocity.x, horizontal * maxSpeed, 0);
}
}
}

// Apply Gravity
void applyGravity(){
// Apply gravity in the down direction relative to the player.
gravity = rigidbody.transform.up * forceOfGravity;
// if we are jumping and begin falling change the force of gravity to be stronger so that we fall faster.
if(jumpClimax){
gravity += (rigidbody.transform.up * fallSpeed);
}
rigidbody.velocity -= gravity * Time.deltaTime * this.transform.localScale.y;
}

// gets the script attached to the bottom of the player.
void Start(){
playerhitbottom = GetComponentInChildren<PlayerHitBottom>();
}

void Update(){
// check if script attached to bottom of player is colliding with a platform
platformCollision = playerhitbottom.getHitting();

// if we are colliding with a platform we are grounded, no longer in air and jumpclimax has not happened.
if(platformCollision){
isGrounded = true;
inAir = false;
jumpClimax = false;
}
// else we are in the air.
else if(!inAir){
inAir = true;
}
// if the player has jumped and their velocity is no longer in the up direction they are not jumping and have reached jumpClimax.
if((rigidbody.velocity.y <= 0.0)  jumping){
jumping = false;
jumpClimax = true;
}

// call movement functions.
Movement();
}
}
``````

Have you tried using FixedUpdate() instead of Update() ? It is recommended for rigidbodies and force handling. Also I noticed you are missing some Time.deltaTimes in some parts which could cause movements to be not smooth.

Is your platform using only one collider, or a bunch side by side?
I had the same problem when I created my level with side by side colliders.
In my 2.5D golf game, my ball would roll along the ground and ‘randomly’ jump up for no reason.
I found it only happened when it met two adjacent colliders. (dynamically created from a ‘Tiled’ map file)
The physics would glitch and send the ball flying upwards.
I’m not sure what to do to fix it, except for changing the code to only generate one collider per ground section.