Currently when I hold space I jump higher than if I tap it, but that is not how I want it. I want it so no matter how long you hold space, It will jump you a certain height.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Walk : MonoBehaviour {
public Rigidbody rb;
public CapsuleCollider col;
public LayerMask jumpableLayers;
public Animator animator;
public float walkSpeed;
public float backwardsWalkSpeed;
public float runSpeed;
public float turnSpeed;
public float jumpPower;
void Update () {
float currentSpeed = 0;
float rotation = 0;
if(Input.GetKey("w")) {
if (Input.GetKey("left shift") && isGrounded()) {
currentSpeed += runSpeed;
if(animator.gameObject.activeSelf)
animator.SetFloat("Walk", 2);
} else {
currentSpeed += walkSpeed;
if (animator.gameObject.activeSelf)
animator.SetFloat("Walk", 1);
}
} else if (Input.GetKey("s")) {
currentSpeed -= backwardsWalkSpeed;
if (animator.gameObject.activeSelf)
animator.SetFloat("Walk", -1);
} else {
if (animator.gameObject.activeSelf)
animator.SetFloat("Walk", 0);
}
if (Input.GetKey("d")) {
rotation += turnSpeed;
} else if (Input.GetKey("a")) {
rotation -= turnSpeed;
}
if(Input.GetKey("space") && isGrounded()) {
Debug.Log("Jumping");
rb.velocity = rb.velocity + new Vector3(0, jumpPower, 0);
}
if (isGrounded()) {
if (animator.gameObject.activeSelf)
animator.SetBool("jumping", false);
} else {
if (animator.gameObject.activeSelf)
animator.SetBool("jumping", true);
}
this.transform.Rotate(new Vector3(0, rotation, 0));
float distance = currentSpeed * Time.deltaTime;
float dx = distance * Mathf.Sin(Mathf.Deg2Rad * rotation);
float dz = distance * Mathf.Cos(Mathf.Deg2Rad * rotation);
rb.AddForce(new Vector3(dx, 0, dz));
}
private bool isGrounded() {
return Physics.CheckCapsule(col.bounds.center, new Vector3(col.bounds.center.x, col.bounds.min.y, col.bounds.center.z), col.radius * 0.90f, jumpableLayers);
}
}
,Currently I have a jumping system working, but if I hold the space bar I just higher than if I tap the space bar. I want the jump height to stay constant the whole time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Walk : MonoBehaviour {
public Rigidbody rb;
public CapsuleCollider col;
public LayerMask jumpableLayers;
public Animator animator;
public float walkSpeed;
public float backwardsWalkSpeed;
public float runSpeed;
public float turnSpeed;
public float jumpPower;
void Update () {
float currentSpeed = 0;
float rotation = 0;
if(Input.GetKey("w")) {
if (Input.GetKey("left shift") && isGrounded()) {
currentSpeed += runSpeed;
if(animator.gameObject.activeSelf)
animator.SetFloat("Walk", 2);
} else {
currentSpeed += walkSpeed;
if (animator.gameObject.activeSelf)
animator.SetFloat("Walk", 1);
}
} else if (Input.GetKey("s")) {
currentSpeed -= backwardsWalkSpeed;
if (animator.gameObject.activeSelf)
animator.SetFloat("Walk", -1);
} else {
if (animator.gameObject.activeSelf)
animator.SetFloat("Walk", 0);
}
if (Input.GetKey("d")) {
rotation += turnSpeed;
} else if (Input.GetKey("a")) {
rotation -= turnSpeed;
}
if(Input.GetKey("space") && isGrounded()) {
Debug.Log("Jumping");
rb.velocity = rb.velocity + new Vector3(0, jumpPower, 0);
}
if (isGrounded()) {
if (animator.gameObject.activeSelf)
animator.SetBool("jumping", false);
} else {
if (animator.gameObject.activeSelf)
animator.SetBool("jumping", true);
}
this.transform.Rotate(new Vector3(0, rotation, 0));
float distance = currentSpeed * Time.deltaTime;
float dx = distance * Mathf.Sin(Mathf.Deg2Rad * rotation);
float dz = distance * Mathf.Cos(Mathf.Deg2Rad * rotation);
rb.AddForce(new Vector3(dx, 0, dz));
}
private bool isGrounded() {
return Physics.CheckCapsule(col.bounds.center, new Vector3(col.bounds.center.x, col.bounds.min.y, col.bounds.center.z), col.radius * 0.90f, jumpableLayers);
}
}