Hey guys I was wondering if you could let me know why this scenario occurs: When I first hit run, my player can jump very high the first time and after that he jumps at a normal height. Also if I move right or left before jumping he moves normal. Here is my code:
//Public Declaration
public float moveSpeed = 5f;
public float jumpSpeed = 15f;
//Private Declaration
private Transform groundCheck;
private bool grounded = true;
void Awake ()
{
//Cache Transform
groundCheck = transform.Find ("groundCheck");
}
void Update ()
{
//Set Grounded
grounded = Physics2D.Linecast (transform.position, groundCheck.position, 1 << LayerMask.NameToLayer ("Ground"));
Movement ();
}
void FixedUpdate ()
{
Jump ();
}
//Functions
void Movement()
{
if (Input.GetKey (KeyCode.D))
transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);
if (Input.GetKey (KeyCode.A))
transform.Translate (-Vector2.right * moveSpeed * Time.deltaTime);
}
void Jump()
{
if ( grounded && Input.GetKeyDown (KeyCode.Space))
rigidbody2D.AddForce (new Vector2 (0f, jumpSpeed));
}
Any help would be greatly appreciated. Thanks in advance!