So I am working on my first 3d project, but my character keeps picking up speed when I hold the movement key down.
I need help making the character stay at the same speed when a button is pushed.
Here’s the Code:
public int speed;
public int jumpForce;
public Transform player;
public Rigidbody rb;
void Update() {
if(Input.GetKeyDown(KeyCode.L))
Debug.Log(canJump);
}
void FixedUpdate()
{
if (rb.velocity.magnitude < .01) {
rb.velocity = Vector3.zero;
}
if (Input.GetButton("Vertical")) {
rb.AddForce(0, 0, Mathf.Round(Input.GetAxis("Vertical")) * speed * Time.deltaTime, ForceMode.Impulse);
}
if (Input.GetButton("Horizontal")) {
rb.AddForce(Mathf.Round(Input.GetAxis("Horizontal")) * speed * Time.deltaTime, 0, 0, ForceMode.Impulse);
}
if (Input.GetButton("Jump")){
if (canJump == true) {
rb.AddForce(0, jumpForce, 0);
}
}
}