Right now I’m just playing around in Unity, and trying out different things. I’m not very good with scripting (yet), so i need help to make my character jump.
The idea is, that the longer you press space (or hold down on a touch screen) the higher the character should jump (with no limits). Holding down longer should make the jump higher.
If anyone could help me making a such script it would be highly appriciated!
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour
{
private bool jumping;
void FixedUpdate()
{
// Are we standing on something, so we can begin jumping?
bool grounded = Physics2D.Raycast(
(Vector2)transform.position - Vector2.up * .25f, // .25f is the distance from the pivot point, where we should start looking for the floor
-Vector2.up,
0.1f // How far should we look
);
// Are we standing on something or are we already jumping
if ((grounded || jumping) && Input.GetKey(KeyCode.Space))
{
jumping = true;
rigidbody2D.AddForce(Vector2.up * 25);
if (rigidbody2D.velocity.y > 200)
{
Vector2 v = rigidbody2D.velocity;
v.y = 200;
rigidbody2D.velocity = v;
}
}
else
{
// If we let go of the jumpbutton, we're not jumping anymore
jumping = false;
}
}
}