Created
December 16, 2019 02:42
-
-
Save JackKell/e11610d89c78b5b2d4046612d3f59af4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class JumpingCharacterControllerExample : MonoBehaviour | |
{ | |
[SerializeField] private float jumpHeight; | |
[SerializeField] private float timeToReachJumpApex; | |
private Vector3 _velocity; | |
private Vector3 _oldVelocity; | |
private float _maxHeightReached = Mathf.NegativeInfinity; | |
private float _startHeight = Mathf.NegativeInfinity; | |
private bool _reachedApex = true; | |
private float Gravity => -2 * jumpHeight / (timeToReachJumpApex * timeToReachJumpApex); | |
private float JumpForce => 2 * jumpHeight / timeToReachJumpApex; | |
private CharacterController _characterController; | |
private bool _isGrounded = false; | |
private float _jumpTimer = 0; | |
private void Start() | |
{ | |
_characterController = GetComponent<CharacterController>(); | |
} | |
private void Jump() | |
{ | |
_jumpTimer = 0; | |
_maxHeightReached = Mathf.NegativeInfinity; | |
_velocity.y = JumpForce; | |
_startHeight = transform.position.y; | |
_reachedApex = false; | |
} | |
private void Update() | |
{ | |
if (_isGrounded && Input.GetButtonDown("Jump")) | |
{ | |
Jump(); | |
} | |
if (!_isGrounded && !_reachedApex) | |
{ | |
_jumpTimer += Time.fixedDeltaTime; | |
} | |
if (!_reachedApex && _maxHeightReached > transform.position.y) | |
{ | |
float delta = _maxHeightReached - _startHeight; | |
float error = jumpHeight - delta; | |
Debug.Log($"jump result: start:{_startHeight:F4}, end:{_maxHeightReached:F4}, delta:{delta:F4}, error:{error:F4}, time:{_jumpTimer:F4}, gravity:{Gravity:F4}, jumpForce:{JumpForce:F4}"); | |
_reachedApex = true; | |
} | |
_maxHeightReached = Mathf.Max(transform.position.y, _maxHeightReached); | |
_oldVelocity = _velocity; | |
_velocity.y += Gravity * Time.fixedDeltaTime; | |
Vector3 deltaPosition = (_oldVelocity + _velocity) * 0.5f * Time.fixedDeltaTime; | |
CollisionFlags collisionFlags = _characterController.Move(deltaPosition); | |
_isGrounded = collisionFlags == CollisionFlags.Below; | |
if (_isGrounded) | |
{ | |
_velocity.y = 0; | |
} | |
} | |
} |
Hey I am glad you like this implementation :D. I have never gotten a GitHub
reply before and I want to be able to answer your questions, but I don’t
know if you will see this reply or not. Message me anything back if you see
this message.
…On Thu, Jul 25, 2024 at 11:43 PM Pando ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Thank you so much for this implementation
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/JackKell/e11610d89c78b5b2d4046612d3f59af4#gistcomment-5133963>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA7F3TLOZB53BPCD7I3HJGDZOH4ZLBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAMBQG4ZDQMRXU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
I made this code for a comment on this video
https://youtu.be/PlT44xr0iW0?si=HVwft4UyggL5CywK . My comment has a ton of
resources and explanation for understanding how it works with links to
videos and code examples. I haven’t worked on this code directly for a
couple of years so I don’t have it on top of mind 😅. But you can feel free
to email me anytime or you can add me on discord at jackkell (my discord
icon is the same as my GitHub icon). We could chat about it and I could
share all my resources with you. I am in PST and can be available to talk
anytime outside of normal working hours.
…On Sat, Jul 27, 2024 at 5:32 PM Pando ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
I can see your message, I just so glad that I found this amazing gist that
save me ton of researching time. As far as I know, there is also formula
for calculating fall gravity separately with fall time as a variable, I
tried it but it didn't work really well when setting to 1 second and above.
Tsym anyway :D
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/JackKell/e11610d89c78b5b2d4046612d3f59af4#gistcomment-5135524>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA7F3TIKT6V4SI3T2SNPBDLZOQ32ZBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAMBQG4ZDQMRXU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
That is where I found this gist . It is really in-depth and also a life-saver since I was desperate finding a solution
Okay, I will dabble and get back to you on this when I get the chance
…On Sun, Jul 28, 2024 at 1:26 AM Pando ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
That is where I found this gist
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/JackKell/e11610d89c78b5b2d4046612d3f59af4#gistcomment-5135732>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA7F3TLUFDZSWKDJQGNJCFLZOSTKXBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAMBQG4ZDQMRXU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for this implementation