I’m making a 2D platformer, and I’d like to make it so that my player character jumps with a default height on jump key down, but jumps higher until a set limit if the jump key is held (just like is the case in most (2D) platformers).
The following are the parts from my player script that are related to jumping (I think I got everything, but please let me know if you spot that something’s missing):
private RigidBody2D playerRigidBody;
private bool isGrounded;
private bool jump;
private Transform[] groundPoints;
private float groundRadius;
private LayerMask whatIsGround;
private float jumpForce;
void Start()
{
playerRigidBody = GetComponent<RigidBody2D>();
}
void Update()
{
HandleInput();
}
void FixedUpdate()
{
isGrounded = IsGrounded();
ResetValues();
}
private void HandleInput()
{
if (isGrounded && jump)
{
isGrounded = false;
playerRigidBody.AddForce(Vector2.up * jumpForce);
}
}
private bool IsGrounded()
{
if (playerRigidBody.velocity <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders*.gameObject != gameObject)*
-
{*
-
return true;*
-
}*
-
}*
-
}*
- }*
- return false;*
}
private void ResetValues()
{
- jump = false;*
}
I’m pretty noob still, both regarding Unity and regarding C#, so the code I have is more or less copied from tutorials on YouTube. I understand most of it, but I wouldn’t yet be able to write it myself from scratch. Just so you have an idea how pedagogical you need to be with me
Any ideas for what code I should add to my script to obtain the functionality I’m looking for?
Thanks!