2D Jump Height Inconsistiency and Framerate Independence
I am relatively new to Unity, and I'm trying to build a 2D platformer. I have a working player controller, but I've noticed that the jump height is sometimes different between the game running in the editor and the actual build. I think this probably has to do with the fact that everything is happening in Update instead of FixedUpdate, and I feel like it might be related to framerate independence? I know that in order to get framerate independence I need to multiply something by Time.deltaTime, but every time I try this in this script, it makes the character unable to move/jump. I think I just need a little clarification on how FixedUpdate/Time.deltaTime work and thoughts on why this inconsistency might be happening.
Below is the working code. Thank you!!
public float maxSpeed; //currently set to 10
public float jumpMax; //currently set to 20 <- this is max velocity
public float initialForce; //currently set to 650
public float acceleration; //currently set to 100
public LayerMask groundLayer;
private bool grounded;
private bool peaked; // true if the player has reached the max jump height
private int jumped = 0; // number of times jumped, for future use in double jumping
private float lookDirection = 1; // used in animation
private Animator animator;
private Rigidbody2D rigidbody;
void Awake()
{
rigidbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
void Update()
{
float move = Input.GetAxis("Horizontal");
if (!Mathf.Approximately(move, 0.0f))
{
lookDirection = move;
lookDirection = (lookDirection / Mathf.Abs(lookDirection));
}
animator.SetFloat("Look", lookDirection);
animator.SetFloat("Speed", Mathf.Abs(rigidbody.velocity.x));
rigidbody.velocity = new Vector2(move * maxSpeed, rigidbody.velocity.y);
if (Input.GetButtonDown("Jump")) //initializes the jump
{
if (grounded)
{
rigidbody.AddForce(Vector2.up * initialForce);
}
}
if (Input.GetButton("Jump")) //allows variable jump heights
{
if (rigidbody.velocity.y < jumpMax && !peaked)
{
if (rigidbody.velocity.y > 0.0001) //prevents continuous jumping
rigidbody.AddForce(Vector2.up * acceleration);
}
if (rigidbody.velocity.y > jumpMax)
{
peaked = true;
}
}
if (rigidbody.velocity.y > 0.0001)
{
animator.SetBool("Ascend", true);
animator.SetBool("Descend", false);
}
else if (rigidbody.velocity.y < -0.0001)
{
animator.SetBool("Descend", true);
animator.SetBool("Ascend", false);
}
else
{
animator.SetBool("Descend", false);
animator.SetBool("Ascend", false);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == ("Ground"))
{
jumped = 0;
grounded = true;
peaked = false;
animator.SetBool("Descend", false);
animator.SetBool("Ascend", false);
animator.SetTrigger("Grounded");
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == ("Ground"))
{
jumped = 1;
grounded = false;
}
}
Your answer
Follow this Question
Related Questions
Jump from the inclined surfaces 0 Answers
2D Side Scroller Help. I have a picture to show you! 0 Answers
I can't do jump in my 2D game 1 Answer
Varible jump heights 2 Answers
My game seems to be running too fast for certain tasks 0 Answers