void Update()
{
if (Input.GetButtonDown("Jump") && isGrounded)
{
jump = true;
jumpForce = 1000;
}
else
{
jump = false;
jumpForce = 0;
}
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, radius, groundLayer);
float h = Input.GetAxis("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(h));
if (h * Fb2d.velocity.x < maxSpeed)
Fb2d.AddForce(Vector2.right * h * moveForce);
if (Mathf.Abs(Fb2d.velocity.x) > maxSpeed)
Fb2d.velocity = new Vector2(Mathf.Sign(Fb2d.velocity.x) * maxSpeed, Fb2d.velocity.y);
if (h > 0 && !facingRight)
Flip();
else if (h < 0 && facingRight)
Flip();
if (jump && isGrounded)
{
if (Fb2d.velocity.y < maxJumpVelocity)
{
Fb2d.AddForce(Vector2.up * jumpForce);
jump = true;
jumpForce = 1000;
}
else
{
// Otherwise stop jumping
jump = false;
jumpForce = 0;
}
}
}
I have no idea what the heck any of that velocity or Vector2 physic stuff does, but I would approach jumping by doing something like this(I did not test this, sorry, I didn’t have much time)
#pragma strict
var player : GameObject; //Assign through inspector
var grounded : boolean;
var jumpForce : float;
function OnCollisionEnter (hit : collision) {
grounded == true; //This should work if the player is touching anything at all, if not then use an if statement ex. if (hit.gameObject.tag == "Ground");
}
function Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
if (grounded) {
var rb = player.GetComponent(Rigidbody);
rb.AddForce(0, jumpForce, 0);
}
}
}
The code is in Javascript so if you want it in C# you may have to change the variables to something like “float jumpForce = 0;” and may have to change the function to void or static OnCollisionEnter. Most of this should be compatible with C# but if you want you are welcome to just copy and paste the code if you can into a Javascript.
Also I wasn’t sure or not if you wanted there to be a time limit between each jump or not like 1 second or 1/2 a seconds. If so you could say “if (grounded && ready) {…}” and then have something in the update stating "if (ready == false) {Time();} then have a function Time() { ready = false; yield WaitForSeconds(0.5); ready = true; }, something along the lines of this should work, sorry for it being so sloppy and all over the place.