#show my how the jump script works

1 messages · Page 1 of 1 (latest)

lavish shard
#

..

#

I cant seem to send the code

#
private void JumpAndGravity()
        {
            if (Grounded)
            {
                // reset the fall timeout timer
                _fallTimeoutDelta = FallTimeout;

                // update animator if using character
                if (_hasAnimator)
                {
                    _animator.SetBool(_animIDJump, false);
                    _animator.SetBool(_animIDFreeFall, false);
                }

                // stop our velocity dropping infinitely when grounded
                if (_verticalVelocity < 0.0f)
                {
                    _verticalVelocity = -2f;
                }

                // Jump
                if (_input.jump && _jumpTimeoutDelta <= 0.0f)
                {
                    Debug.Log("JUMPED");
                    // the square root of H * -2 * G = how much velocity needed to reach desired height
                    _verticalVelocity = Mathf.Sqrt(JumpHeight * -2f * Gravity);

                    // update animator if using character
                    if (_hasAnimator)
                    {
                        _animator.SetBool(_animIDJump, true);
                    }
                }

                // jump timeout
                if (_jumpTimeoutDelta >= 0.0f)
                {
                    _jumpTimeoutDelta -= Time.deltaTime;
                }
            }
#
else
            {
                // reset the jump timeout timer
                _jumpTimeoutDelta = JumpTimeout;

                // fall timeout
                if (_fallTimeoutDelta >= 0.0f)
                {
                    _fallTimeoutDelta -= Time.deltaTime;
                }
                else
                {
                    // update animator if using character
                    if (_hasAnimator)
                    {
                        _animator.SetBool(_animIDFreeFall, true);
                    }
                }

                // if we are not grounded, do not jump
                _input.jump = false;
            }

            // apply gravity over time if under terminal (multiply by delta time twice to linearly speed up over time)
            if (_verticalVelocity < _terminalVelocity)
            {
                _verticalVelocity += Gravity * Time.deltaTime;
            }
        }
#

ok yeah here you go

#

its the unity's starter asset if you want to check it out for yourself but here is the code

sharp zephyr
#

unity starter is an example of how unity works, it has a lot of flaws

#

basically the thing happening here is your character keep jumping because it's never in airborn

lavish shard
#

well thats disappointing

sharp zephyr
#

you have to fix that yourself by changing the hitbox of the character

lavish shard
#

what do you recommend?

#

changing the entire character controller or should I try to fix that?

sharp zephyr
#

what i did is remake all the jump and landing scripts

#

and also adjust the landing hitbox to make it never larger than the player hitbox

#

One easy solution to fix it : put a timer between each jumps

#

variables

#

[SerializeField] private float TimeInAir = 0f;
[SerializeField] private float TimeToJump = 0.5f;

lavish shard
#

there is already a time to jump I think

#

its set to like .5

#

yeah its called jumptimout

#
        public float JumpTimeout = 0.50f;```
sharp zephyr
#

alright so

#

you jump only once and then it keeps jumping again even if you don't press jump ?

lavish shard
#

yeah I just need to press W pressing any other key will stop the auto jump

sharp zephyr
#

the _input.jump

#

is it on the key binding ?

lavish shard
#

its linked to the StartAssetsInputs

_input = GetComponent<StarterAssetsInputs>();```
sharp zephyr
#

this only happens if you jump while being stuck on a wall ?

lavish shard
#

so basically I go to the slightly slanted wall, I just once and press W, after that the character will keep jumping

#

if the wall is not perfectly 90 degrees I am able to climb it

sharp zephyr
#

but what if you jump anywhere else, in the middle of the road for example

#

alright

#

the thing i did is use the tags

lavish shard
sharp zephyr
#

i put "walkable" as a tag for all my roads, but never on buildings or on places i don't want the character to go

lavish shard
#

is it the same if I use layers?

sharp zephyr
#

and in the jumping / running / walking script, in code i used "OnTriggerStay"

#

i prefer tags

#

but layers can work, never tryed

#

if the character OnTriggerStay Collider.tag or Collider.name == "Walkable" then he can walk/run/jump

#

private void OnTriggerStay(Collider m_otherCollider)
{
if (m_otherCollider.name == "Walkable")
{
CharacterGroundedInformations.M_IsGrounded = true;
my_character_first_jump.M_OtherJump = 1;
IsAirborn = false;
}
}

lavish shard
#

I tried layers and basically the character gets stuck on the wall, doesnt auto jump anymore but stays there stuck in "falling" animation

sharp zephyr
#

i used my own variables in there, you have to use yours

sharp zephyr
#

or else you can use the raycast

lavish shard
sharp zephyr
#

basically you make your character throw multiple raycasts around him, and if it a wall too close, then it cannot got toward it anymore

sharp zephyr
lavish shard
#

alright thanks ill try that