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 dcrsSpeedMltpr; //decreaseSpeedMultiplier (changeable) 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 LayerMask Groundmask; //Groundmask for Jump Bool 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 { playerRigidBody.AddForce(transform.forward * speed * 1.75f); 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); } 
maxspeedanddcrsSpeedMltpr? What output are you seeing in your log? The following two lines are going to be fighting for dominance: (inMove):speed = speed - dcrsSpeedMltpr * Time.deltaTime;and then (inRolling):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. \$\endgroup\$MovePositionand 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? \$\endgroup\$