I am trying to detect if the player is falling by using the negative velocity.y value.
The y value in the following line of code should change continuously if the player is jumping up and down, but the y value changed for a bit and stayed the same value when the player was jumping up and down. I don’t know why.
Debug.Log (“y =” + rigidbody2D.velocity.y);
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
//player movement
public float movementSpeed = 5.0f;
private int jumpHeight = 250;
private bool isGrounded = false;
void Update () {
Debug.Log ("y =" + rigidbody2D.velocity.y);
if (isGrounded) {
if (Input.GetButtonDown ("Jump") || Input.GetMouseButtonDown(0)) {
Jump ();
}
}
}
void Jump(){
if (!isGrounded) { return;
}
rigidbody2D.AddForce (new Vector2 (0, jumpHeight), ForceMode2D.Force);
}
void OnCollisionEnter2D(Collision2D collision)
{
isGrounded = true;
Jump ();
}
void OnCollisionExit2D(Collision2D collision){
isGrounded = false;
}
}