![]() If the Update method is constantly looking for the ground, it causes input problems and makes ground detection buggy. This way, I’m only firing rays when I absolutely need to know where the ground is. Once we’re clear of the ground, the coroutine signals that a sufficient wait has passed and the Update method starts looking for the ground again, sets the grounded bool variable to whatever the Grounded() method finds and turns itself off again. A shorter ray fails to detect the ground properly, so this is a compromise measure. Originally I thought a single frame would do it (thus the name of the coroutine), but it turned out that a tenth of a second was necessary to get out of ray range. This coroutine keeps the Update method from checking for the ground until we’ve had time to actually clear the ground. Last in the OnJump() method, we call the JumpFrameSkipRoutine. We know we’ve successfully jumped, so we can safely declare ourselves off the ground without checking. We set a grounded bool variable to false - we don’t want to fire another ray to confirm that, as we’re not very far off the ground in the first frame. As a bonus, you'll also get all of the mesh assets I used (cactus, desert landscape, and rocks) and a little dirt emitter for when the cactus moves. We then apply velocity to the rigidbody based on the jumpForce variable. You'll get all source code, a Unity project with a simple test scene, and the character controller from the tutorial, all set up and ready to go. ![]() If the Raycast finds a collider below the player on layerMask 8 (my ground layer), then the function returns true. ![]() Picking this apart a bit: you can see that we first check to see if the player is grounded when a jump is attempted.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |