Hi i am new to coding and need help with jumping becuase my ch flyes
public class Move : MonoBehaviour { public float speed; public float jumpForce; private float moveInput;
private Rigidbody2D rb;
private bool facingright = true;
private bool isGrounded;
public Transform groundcheck;
public float checkRadius;
public LayerMask WhatIsGround;
private int extraJumps;
public int extraJumpsValue;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundcheck.position, checkRadius, WhatIsGround);
moveInput = Input.GetAxis("Horizontal");
Debug.Log(moveInput);
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingright == false && moveInput > 0)
{
Flip();
}
else if (facingright == true && moveInput < 0)
{
Flip();
}
}
void Update()
{
if (isGrounded == true)
{
extraJumps = 2;
}
if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}
void Flip()
{
facingright = !facingright;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
, public class Move : MonoBehaviour { public float speed; public float jumpForce; private float moveInput;
private Rigidbody2D rb;
private bool facingright = true;
private bool isGrounded;
public Transform groundcheck;
public float checkRadius;
public LayerMask WhatIsGround;
private int extraJumps;
public int extraJumpsValue;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundcheck.position, checkRadius, WhatIsGround);
moveInput = Input.GetAxis("Horizontal");
Debug.Log(moveInput);
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingright == false && moveInput > 0)
{
Flip();
}
else if (facingright == true && moveInput < 0)
{
Flip();
}
}
void Update()
{
if (isGrounded == true)
{
extraJumps = 2;
}
if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}
void Flip()
{
facingright = !facingright;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
Why do you mean by "flies"? Does the character constantly fly upwards? Does your character hover above the ground?
Answer by idans555 · Jan 16, 2020 at 12:03 PM
no, when I jump pressing space button I can click as much as I want and the character won't stop jumping @LeonardSchoyen
Your answer
Follow this Question
Related Questions
How to make my controller jump when only on ground and not the walls? 0 Answers
Physics.Raycast not working 1 Answer
Decreasing Gravity Under Certain Conditions Problem 0 Answers
Air Control While Jumping? 1 Answer
jumping for different characters? 0 Answers