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; | |
} | |
} | |
} |
Author
JackKell
commented
Jul 28, 2024
via email
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