# Unity Rigidbody stops directly after moving

I'm creating a skating game, my Idea now for moving was when I move and then dont press any button anymore to move, it should still roll a bit and decrease momentum after a short while to fully stop. My problem now is when I dont press any buttons anymore after moving, it stops automaticly.

I came up with the idea of giving the rigidbody a force to the direction of the player, what is kinda working like I want to have it but its not realy connected with the momentum, it stops a little bit in the air or while I am skating then slowly going to stop. I want to use the momentum of my move Float "speed" to get keep the momentum and slowly decrease till it stops.

This is my code I came up with. I COMMENTED THE CODE ARE WHAT I CAME UP WITH LOOK ON "void Rolling" FUNCTION.

//MOVEMENT FLOATS
public float speed; //starting Speed float
public float maxspeed; // Maximum Speed float (changeable)
private float minspeed = 0;
public float runSpeed;
public float turnSmoothing; //Smoothness of rotating (changeable)
public float jumpSpeed; //Speed of the Jump (changeable)

public GameObject rayCastGround; //Raycasting if Player is onGround or not
private Vector3 movement; //Sets the movement
private Vector3 targetDirection;
private Rigidbody playerRigidBody;
private Animator anim;

public bool isGrounded;
public bool isSkating = false;

void Awake()
{
playerRigidBody = GetComponent<Rigidbody> ();
anim = GetComponent<Animator>();
speed = 0f;
}

void FixedUpdate()
{
float lh = Input.GetAxisRaw ("Horizontal");
float lv = Input.GetAxisRaw ("Vertical");

Jump ();
Move (lh, lv);
Rolling ();

if(lh > 0 || lv > 0 || lh < 0 || lv < 0 )
{
if(isGrounded)
{
anim.Play("SkatingAnim");
}
}
else
{
if(isGrounded && speed == 0f)
{
anim.Play ("SkatingIdleAnim");
}
}
}

void Move (float lh, float lv)
{
movement.Set (lh, 0f, lv);
//movement = Camera.main.transform.TransformDirection(movement);

//RUNSPEED
if (Input.GetKey (KeyCode.LeftShift) && isGrounded)
{
movement = movement.normalized * runSpeed * Time.deltaTime;
}
//SET THE SPEED OF MAX AND MIN SPEED!
else
{
if(Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.S))
{
speed = speed + dcrsSpeedMltpr * Time.deltaTime;
if(speed >= maxspeed)
{
speed = maxspeed;
}

isSkating = true;
if(isSkating)
{
anim.SetBool ("isOnSkating", true);
}
}
else
{
isSkating = false;
if(!isSkating)
{
anim.SetBool("isOnSkating", false);
}

speed = speed - dcrsSpeedMltpr * Time.deltaTime;
if(speed <= minspeed)
{
speed = minspeed;
}
}
anim.SetFloat("Skatespeed", speed);
}

movement = movement.normalized * speed * Time.deltaTime;

playerRigidBody.MovePosition (transform.position + movement);

if (lh != 0f || lv != 0f)
{
Rotating(lh, lv);
}
}
//HERE IS THE CODE I CAME UP WITH ADDFORCE ON RIGIDBODY IT SHOULD ROLL A BIT AFTER STOPPING MOVING TILL THE MOMENTUM IS ZERO.
void Rolling()
{
//MOVES A LITTLE BIT FURTHER WHEN PLAYER STOPS SKATING
AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo(0);
if(!isSkating) //<- WHEN THE PLAYER STOPS SKATING IT GOES TO ROLL ANIMATION AND SHOULD THEN DECREASE ITS MOMENTUM TILL ZERO
{
Debug.Log(speed);
}
}

void Rotating (float lh, float lv)
{
targetDirection = new Vector3 (lh, 0f, lv);

Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up);

Quaternion newRotation = Quaternion.Lerp (GetComponent<Rigidbody> ().rotation, targetRotation, turnSmoothing * Time.deltaTime);

playerRigidBody.MoveRotation(newRotation);
}

• What values are you using for maxspeed and dcrsSpeedMltpr? What output are you seeing in your log? The following two lines are going to be fighting for dominance: (in Move): speed = speed - dcrsSpeedMltpr * Time.deltaTime; and then (in Rolling): playerRigidBody.AddForce(transform.forward * speed * 1.75f);. Also in one method you're physically moving the object, while in the other you're applying a force. – Zhaph - Ben Duguid Jan 22 '18 at 9:57
• The Maxspeed is there for the speed that it doesnt go further than the maxspeed, if I press WASD the speed goes from 0 to maxspeed, but when I dont press WASD the speed goes down to minspeed that is set to 0 but it takes a little time to reach 0 and also to reach maxspeed. The dcrsSpeedMltpr is just a little number that the speed doesnt go up that fast. And I know I was applying a force to the rb but I didnt come up with any other Idea, maybe someone could help me on that. – Daniel Jan 22 '18 at 12:33
• You're right - the use of both MovePosition and and forces don't seem to be an issue - and your code seems to work for me (dropped onto a sphere with a Rigidbody) - have you added Drag or Gravity constraints to your rigid-body that are stopping it sooner than you expect? – Zhaph - Ben Duguid Jan 22 '18 at 13:04

You should consider adding a Physic Material to your skate and bring down the Dynamic and Static frictions.

Debugging is difficult and you have a lot of things going on in your script. Something could be interfering with something else. I suggest you strip it down and slowly add things back to it. Start with something like the below script that will move a cube left and right, pressing he up and down arrows. Once you have the deisred behavior, then add more of your script and see where it is breaking it.

public float force;

private Rigidbody rg;

void Start()
{
rg = GetComponent<Rigidbody>();
}

void FixedUpdate()
{
if (Input.GetKey(KeyCode.UpArrow))
{