- Home /
Inconsistent jump height
So I am working on a 2D platformer game on Android. At the start, my character jumps normally but whenever the score reaches about to 20 (the score is based on time) my character seems to be jumping higher that it should.
I've searched some solutions such as using Mathf.Clamp
but it still doesn't work. I've already added a boolean variable for being grounded but still, it isn't working.
Here's my code. I'm using Standard Assets as I've watched on a short YouTube video.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class Control : MonoBehaviour
{
//movement
rb = GetComponent<Rigidbody2D>();
public void Update()
{
if (grounded)
{
rb.velocity = Vector3.zero;
rb.angularVelocity = 0f;
}
if (CrossPlatformInputManager.GetButtonDown("Jump") && grounded)
{
rb.AddForce(Vector2.up * 600f);
}
}
public void OnTriggerStay2D(Collider2D col)
{
if (col.gameObject.tag.Equals("tall box") || col.gameObject.tag.Equals("short box") || col.gameObject.tag.Equals("small box") || col.gameObject.tag.Equals("platform"))
grounded = true;
}
public void OnTriggerExit2D(Collider2D col)
{
grounded = false;
}
}
I've also tried to add the Mathf.Clamp
and added it in the Update
but it still doesn't work.
Here's the code
rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -4f, 4f));
I would really appreciate if this gets answered, this is my first ever project in unity and I'm hoping that it would turn out well.
Answer by ianberana123 · Oct 01, 2020 at 07:50 AM
I made an oopsie, turns out the code was correct. Its just that the canvas where my player is is set to Screen Space - Camera or it maybe the script for my camera to follow the player. What I did is to set my canvas to Screen Space - Overlay or World Space. Then I made a script for my objects within that canvas to follow the cam. Either way, the code I posted was fine and everything is good now. Hope this post helps so I didn't delete it.
Your answer
Follow this Question
Related Questions
Inconsistent jump height 1 Answer
HOLD JUMP BUTTON TO JUMP HIGHER 2 Answers
Multiple Cars not working 1 Answer
HOLD THE JUMP BUTTON TO JUMP HIGHER 0 Answers
2D multi touch 1 Answer