- Home /
when attempting to jump my player shoots into the air and disappears. can someone help?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Jump : MonoBehaviour { public float speed; public float Jumpforce; private float moveinput; private Rigidbody2D Myrb2d;
private bool facingRight = true;
private bool isGrounded;
public Transform groundcheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJumps;
public int extraJumpsValue;
// Start is called before the first frame update
void Start()
{
extraJumps = extraJumpsValue;
Myrb2d = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate(){
isGrounded = Physics2D.OverlapCircle(groundcheck.position, checkRadius, whatIsGround);
moveinput = Input.GetAxis("Horizontal");
Myrb2d.velocity = new Vector2(moveinput * speed, Myrb2d.velocity.y);
if (facingRight == false && moveinput > 0){
Flip();
}else if (facingRight == true && moveinput < 0){
Flip();
}
}
private void Update(){
if(isGrounded == true){
extraJumps = extraJumpsValue;
}
if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0) {
Myrb2d.velocity = Vector2.up * Jumpforce;
extraJumps--;
isGrounded = false;
} else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true) {
Myrb2d.velocity = Vector2.up * Jumpforce;
}
}
void Flip(){
facingRight = !facingRight;
Vector3 scaler = transform.localScale;
scaler.x *= -1;
transform.localScale = scaler;
}
}
Comment