My player is a ball and it keeps moving even if I’m not pushing a button. How to make it slow down when I stop pushing a movement button?
Here is my movement code:
using UnityEngine;
using System.Collections;
public class BallMovement : MonoBehaviour {
public float speed;
public float jumpSpeed;
bool CanJump;
bool jump;
void Start ()
{
CanJump = true;
jump = false;
}
void OnCollisionExit(Collision collisionInfo)
{
CanJump = false;
jump = false;
}
void OnCollisionEnter(Collision collisionInfo)
{
CanJump = true;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && CanJump == true)
{
jump = true;
}
}
void FixedUpdate () {
float moveHorizontal = Input.GetAxis("Horizontal");
// float moveVertical = 0.0f;
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce(movement * speed * Time.deltaTime);
if (jump == true)
{
rigidbody.AddForce(Vector3.up * jumpSpeed);
}
}
}