Hi, I am having trouble making a jump which allows the player to either hold the screen to jump continuously or tap the screen once and just jump once. My script below is what I am using but since GetMouseButton(0) is being called more than once while the player is on the ground the rigid body is getting the force added more than once too which means the player flies way up into the air. Is there a way which I can call the rigidbody.AddForce() just once using this method or will I need to use a different method? thanks
var jump : boolean;
var grounded : boolean = false;
var groundCheck : Transform;
var groundRadius : float = 0.2f;
var whatIsGround : LayerMask;
var myRigidbody : Rigidbody2D;
function FixedUpdate(){
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
}
function Update(){
if(Input.GetMouseButton(0)){
if(grounded){
jump = true;
}
}
if(jump){
myRigidbody.AddForce(new Vector2(0, 1500f * Time.fixedDeltaTime));
jump = false;
}
}
↧