I want to make my player jump when I press the space bar and it is on the ground. Then after I’m off the ground, fall back to the ground. Previously I was able to, but i turned my character into a kinetic rigidbody, and don’t know how to get it to work. My current code is the following.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
//Variables
public GameObject player;
public float moveSpeed;
public float turnSpeed;
private float jumpValue = 0;
private float moveHorizontal;
private float moveVertical;
bool spaceKeyDown = false;
bool isGrounded = false;
void OnCollisionEnter (Collision col)
{
if(col.gameObject.tag == "Floor")
{
isGrounded = true;
}
}
void OnCollisionExit (Collision col)
{
if(col.gameObject.tag == "Floor")
{
isGrounded = false;
}
}
void Update ()
{
moveHorizontal = Input.GetAxis ("Horizontal");
moveVertical = Input.GetAxis ("Vertical");
if(Input.GetButtonDown("Jump"))
{
spaceKeyDown = true;
//Debug.Log("Space Key Pressed. Frame: " + Time.frameCount);
}
}
void FixedUpdate ()
{
JumpData ();
transform.Rotate(Vector3.up * moveHorizontal * turnSpeed * Time.deltaTime);
transform.Translate(Vector3.forward * moveVertical * moveSpeed * Time.deltaTime);
}
void JumpData()
{
if (isGrounded == true)
{
if (spaceKeyDown == true)
{
spaceKeyDown = false;
jumpValue = 9.8f;
}
else {jumpValue = 0;}
}
else {jumpValue = 0;}
spaceKeyDown = false;
}
}