Right now I’m trying to create something akin to a platformer where blocks are randomly falling from the sky and you’re suppose to jump on them to reach an object near the roof. Obviously to do this I need the player character to be able to jump, but there are a considerable amount of problems that keep popping up and nothing I’ve done to try and quash them all has worked.
This is the current version of the jumping code I have:
//The code above this simply deals with the D-Pad movement
if (Input.GetKeyDown(KeyCode.Space) && grounded == true)
{
//FORCE is a constant I used to define how high the player is suppose to jump. It's only suppose to go high enough to jump on one cube
rfRef.AddForce(new Vector3(0, FORCE, 0), ForceMode.Impulse);
grounded = false;
}
}
void FixedUpdate()
{
Vector3 origin = new Vector3(transform.position.x, transform.position.y - 0.515f, transform.position.z);
Vector3 direction = transform.TransformDirection(Vector3.down);
print(grounded);
//rayDistance is, naturally, suppose to be a public float so I can find out what length for the ray length works. Currently it is set to 0.2f
if (Physics.Raycast(origin, direction, rayDistance) == true)
{
grounded = true;
}
}
The idea behind the code is SUPPOSE to be that the player jumps and a physics ray looks directly beneath the player to see if there’s anything in range below it and nowhere else. Naturally this is suppose to include cubes as much as the ground itself. If there is, then the player is considered grounded and can jump again. However, for whatever reason, the code allows the player to double jump before saying it’s no longer grounded (Sometimes it will land and permanently say it’s not grounded, but I have no idea what’s causing that). Is there a reason for this? Is there something I’m missing?