I am trying to get my character to jump, but when the character jumps, it moves up about 50px above the ground, and doesn't come back down, and I cannot figure out why this is happening. Here is the code that I am using on the character
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float maxSpeed = 100f;
public float moveForce = 100f;
public float jumpForce = 100f;
public float groundRadius = 0.2f;
public Transform groundCheck;
public LayerMask whatIsGround;
protected bool jump = false;
public bool grounded = false;
void Update(){
if(Input.GetButtonDown("Jump") && this.grounded){
this.jump = true;
}
}
// Update is called once per frame
void FixedUpdate(){
this.grounded = Physics2D.OverlapCircle(this.groundCheck.position, this.groundRadius, this.whatIsGround);
if(rigidbody2D.velocity.x < this.maxSpeed)
rigidbody2D.AddForce(transform.right * this.moveForce); //maxSpeed);
if(Mathf.Abs(rigidbody2D.velocity.x) > this.maxSpeed)
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * this.maxSpeed, rigidbody2D.velocity.y);
if(this.jump){
rigidbody2D.AddForce(new Vector2(0f, this.jumpForce));
this.jump = false;
}
}
}
↧