Hi there! I’m using this script for player (ball) movement and I’m using colliders to check if a player can jump or he cannot. But this colliders doesn’t work always so as a result I have a situation when player IS on a ground but he CAN NOT jump. Any ideas? Thanks!
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 = 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);
}
}
}