I need to know how to make a simple jump script so i can make 3D game where you can jump.
public float jumpHeight = 7f;
public bool isGrounded;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (isGrounded)
{
if (Input.GetButtonDown("Jump"))
{
rb.AddForce(Vector3.up * jumpHeight)
}
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
void OnCollisionExit(Collision other)
{
if (other.gameObject.tag == "Ground")
{
isGrounded = false;
}
}
Make sure to add a tag with the name “Ground” onto the ground object and your done.
**Warning : ** Untested code … A direct copy N paste may make you to to ask a new question on this forum
Add gameobject, add Rigidbody to it, add script to it … and in that script add this
public int forceConst = 50;
private bool canJump;
private RigidBody selfRigidbody;
void Start(){
selfRigidbody = GetComponent<RigidBody>();
}
void FixedUpdate(){
if(canJump){
canJump = false;
selfRigidbody.addForce(0, forceConst, 0, ForceMode.Impulse);
}
}
void Update(){
if(Input.GetKeyUp(Keycode.SPACE)){
canJump = true;
}
}
Take a look at AddForce