Huy guise, I’m in the process of teaching myself Unity (2D), and I have a bit of a problem…
I only have two script files, all attached to the player gameobject sprite thing. One of them is a left and right movement script, and the other one allows jumping. The jump script is the problematic one:
public class Jump : MonoBehaviour
{
public bool YN = false;
public float jmppwr = 10f;
void Update()
{
RaycastHit2D ground = Physics2D.Raycast(transform.position, -Vector2.up, 1);
if (ground != null)
{
YN = true;
}
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.Space) && YN)
{
rigidbody2D.AddForce(new Vector2(0f, jmppwr));
YN = false;
}
}
}
The issue is that bool YN is always true (even when there’s no ground collision object underneath), hence allowing the player to jump mid-air and stuff.
Anyone know what the problem is?