My jumping in my game is inconsistent
My characters jumps are inconsistent, sometimes I have to press spacebar multiple times for it to jump while other times it only takes one try. I assumed it was because I was moving so I added more functionality into the if statement in the handleInput function to try and cope with it, but this does not fix it. Another problem I am having is after I jump sometimes after I land I get stuck in place, but if I jump again and land it unsticks itself, very strange :( . Please help. Here is my code:
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour { private Rigidbody2D RigidBody; //A private varible for RigidBody2D to be stored in
private Animator Animation;
//Varible to store the animator from unity
[SerializeField]
private float movementSpeed;
//Varible to store speed of the player
private bool FacingRight;
[SerializeField]
private Transform[] GroundPoints;
[SerializeField]
private float GroundRadius;
[SerializeField]
private LayerMask WhatIsGround;
private bool isGrounded;
private bool jump;
[SerializeField]
private bool AirControl;
[SerializeField]
private float jumpForce;
// Use this for initialization
void Start() {
FacingRight = true;
RigidBody = GetComponent<Rigidbody2D>();
//Getting RigidBody2D and storing it in the RigidBody varible
Animation = GetComponent<Animator>();
//Getting animator from unity and storing it in the animation varible
}
// Update is called once per frame
void FixedUpdate()
{
isGrounded = IsGrounded();
float Horizontal = Input.GetAxis("Horizontal");
//Creates a reference for Unity called Horizontal
Movement(Horizontal);
//Calls the movement function
Flip(Horizontal);
HandleInput();
}
private void Movement(float Horizontal)
{
if (isGrounded || AirControl)
{
RigidBody.velocity = new Vector2(Horizontal * movementSpeed, RigidBody.velocity.y);
}
//Makes the player go either left or right depending what key is pressed leaving the y unchanged
Animation.SetFloat("Speed", Mathf.Abs(Horizontal));
//Sets the Speed parameter in unity to the horizontal, however the math function makes sure it always
//positive so it doesn't return a negative speed
if (isGrounded && jump)
{
isGrounded = false;
RigidBody.AddForce(new Vector2(0, jumpForce));
jump = true;
jump = false;
}
}
private void HandleInput()
{
if ((Input.GetKeyDown(KeyCode.Space) && Input.GetKeyDown(KeyCode.LeftArrow)) || (Input.GetKeyDown(KeyCode.Space) && Input.GetKeyDown(KeyCode.RightArrow)) || ((Input.GetKeyDown(KeyCode.Space) && Input.GetKeyDown(KeyCode.D)) || (Input.GetKeyDown(KeyCode.Space) && Input.GetKeyDown(KeyCode.A)) || (Input.GetKeyDown(KeyCode.Space))))
{
jump = true;
Debug.Log("Space key was pressed.");
}
}
private void Flip(float Horizontal) {
if (Horizontal > 0 && !FacingRight || Horizontal < 0 && FacingRight) {
FacingRight = !FacingRight;
// If the Sprite is facing right and the FacingRight varible is not true or
//it is facing left and the FacingRight varible is set to true it will make the
//FacingRight varible the opposite of what it was before ie. False to true or true to false
Vector3 TheScale = transform.localScale;
//Gets the scale from unity and stores it in a varible so it can refrenced
TheScale.x *= -1;
//The scale is made the inverse of what it was before (1 to -1 or -1 to 1) flipping the Sprite
transform.localScale = TheScale;
}
}
private bool IsGrounded()
{
if (RigidBody.velocity.y <= 0)
//if the player is falling or still
{
foreach (Transform point in GroundPoints)
{
Collider2D[] Colliders = Physics2D.OverlapCircleAll(point.position, GroundRadius, WhatIsGround);
for (int i = 0; i < Colliders.Length; i++)
{
if (Colliders[i].gameObject != gameObject)
{
return true;
}
}
}
}
return false;
}
}
Answer by carringtonz · Dec 14, 2016 at 01:02 PM
I fixed it, I didn't call handleinput in update, I called it in fixedupdate. You have to make a separate function called update, because fixedupdate doesn't run every frame, hence the reason why my jumps were so inconsistent.