- Home /
Question by
codedbythet · Nov 09, 2020 at 11:35 PM ·
programmingjumpjumpingscriptingproblem
2d Jumping Problem
I am having a problem trying to fix a jump to a single jump with a double jump as a powerup. I have this code but I am constantly jumping.
How can I fix this and add a double jump?
Character3D Controller
public class CharacterController2D : MonoBehaviour
{
[SerializeField] private float m_JumpForce = 400f;
[Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f;
[SerializeField] private bool m_AirControl = false;
[SerializeField] private LayerMask m_WhatIsGround;
[SerializeField] private Transform m_GroundCheck;
[SerializeField] private Transform m_CeilingCheck;
[SerializeField] private Collider2D m_CrouchDisableCollider;
const float k_GroundedRadius = .2f;
private bool m_Grounded;
const float k_CeilingRadius = .2f;
private Rigidbody2D m_Rigidbody2D;
private bool m_FacingRight = true;
private Vector3 m_Velocity = Vector3.zero;
[Header("Events")]
[Space]
public UnityEvent OnLandEvent;
[System.Serializable]
public class BoolEvent : UnityEvent<bool> { }
private void Awake()
{
m_Rigidbody2D = GetComponent<Rigidbody2D>();
if (OnLandEvent == null)
OnLandEvent = new UnityEvent();
}
private void FixedUpdate()
{
bool wasGrounded = m_Grounded;
m_Grounded = false;
Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
m_Grounded = true;
if (!wasGrounded)
OnLandEvent.Invoke();
}
}
}
public void Move(float move, bool jump)
{
Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
if (move > 0 && !m_FacingRight)
{
Flip();
} else if (move < 0 && m_FacingRight)
{
Flip();
}
if (m_Grounded && jump)
{
m_Grounded = false;
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
}
}
private void Flip()
{
m_FacingRight = !m_FacingRight;
transform.Rotate(0f, 180f, 0f);
}
}
And the Player Movement
public CharacterController2D controller;
public Animator animator;
public float runSpeed = 40f;
float horizontalMove = 0f;
bool jump = false;
// Update is called once per frame
void Update () {
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
if (Input.GetButtonDown("Jump"))
{
jump = true;
// animator.SetBool("IsJumping", true);
}
}
public void OnLanding ()
{
// animator.SetBool("IsJumping", false);
}
void FixedUpdate ()
{
// Move our character
controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
jump = false;
}
}
Comment
Answer by logicandchaos · Nov 11, 2020 at 03:53 AM
to add in double jumps you need to add in a variable, int, to keep track of jumps. When you jump instead of checking m_GroundCheck you check if jumps>0 then you jump--; and when you set m_GroundCheck to true you also set jumps to 2.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to force a player to jump? 2 Answers
Coyote Time/Ledge Assistance Causes Double Jump 4 Answers
Jumping not always work 2 Answers
Box collider didn't jump with player 1 Answer