Hi, I have a code which is basically perfect for jump and doublejump but the character has a fixed jumping height. What I’m looking for is a “super mario jumping effect”. Press the jump button really fast, jump a little, keep it pressed , jump a little higher and so on. (hope I explained myself well, if not, I’ll try again).
this is the code
using UnityEngine;
using System.Collections;
public class CharController : MonoBehaviour
{
[SerializeField] private float m_JumpForce;
[SerializeField] private float moveSpeed;
[SerializeField] private LayerMask m_WhatIsGround;
private Transform m_GroundCheck;
private Transform m_GroundCheckS;
const float k_GroundedRadius = .15f;
private bool cir_Grounded;
private bool cir_GroundedS;
private bool is_Grounded;
private bool doubleJump;
private Rigidbody2D m_Rigidbody2D;
// Use this for initialization
void Start()
{
m_GroundCheck = transform.Find("groundCheck");
m_GroundCheckS = transform.Find("groundCheckS");
m_Rigidbody2D = GetComponent<Rigidbody2D>();
gameObject.transform.position = new Vector2 (10, 30);
}
void Update()
{
cir_Grounded = false;
cir_GroundedS = false;
is_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)
cir_Grounded = true;
}
Collider2D[] colliders2 = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders2.Length; i++)
{
if (colliders2[i].gameObject != gameObject)
cir_GroundedS = true;
}
if (cir_GroundedS && cir_Grounded)
{
is_Grounded = true;
}
//m_Rigidbody2D.velocity = new Vector2 (moveSpeed, m_Rigidbody2D.velocity.y); //Moves the player horizontally
if (Input.GetKeyDown ("space"))
{
if (is_Grounded)
{
is_Grounded = false;
cir_Grounded = false;
cir_GroundedS = false;
m_Rigidbody2D.velocity = new Vector2(m_Rigidbody2D.velocity.x, 0);
m_Rigidbody2D.AddForce (new Vector2(0f, m_JumpForce));
doubleJump = true;
}
else if (doubleJump)
{
m_Rigidbody2D.velocity = new Vector2(m_Rigidbody2D.velocity.x, 0);
m_Rigidbody2D.AddForce (new Vector2(0f, m_JumpForce));
doubleJump = false;
}
}
}
}
Thanks for the attention