Jetpack vr script help

(TikTok - Make Your Day)

(TikTok - Make Your Day)

When i land from a high place or try to boost right before i hit the ground this happens in the video. I start like bouncing up and down and have strong gravity. Also if i use my left joystick to move in the air it makes me fall fast and cancels the slow fall.Can someone tell me how to fix it?

Script:


private void Awake()
    {
        characterController = GetComponent<CharacterController>();
        xrOrigin = GetComponent<XROrigin>();
        jetpackAction.action.Enable();
        moveAction.action.Enable();
        currentFillRatio = 1f;
        if (jetpackTrail != null)
        {
            jetpackTrail.emitting = false;
        }
    }

    private void OnDestroy()
    {
        jetpackAction.action.Disable();
        moveAction.action.Disable();
    }

    private void Update()
    {
        isGrounded = IsGrounded();
        Vector2 jetpackInput = jetpackAction.action.ReadValue<Vector2>();
        Vector2 moveInput = moveAction.action.ReadValue<Vector2>();

        HandleHorizontalMovement(moveInput);
        HandleVerticalMovement(jetpackInput.y);
        ApplyMovement();
    }

    private void HandleHorizontalMovement(Vector2 moveInput)
    {
        if (moveInput.sqrMagnitude > 0.01f)
        {
            Vector3 forward = xrOrigin.Camera.transform.forward;
            Vector3 right = xrOrigin.Camera.transform.right;
            forward.y = 0f;
            right.y = 0f;
            forward.Normalize();
            right.Normalize();
            Vector3 move = (forward * moveInput.y + right * moveInput.x) * moveSpeed;
            velocity.x = move.x;
            velocity.z = move.z;
        }
        else
        {
            velocity.x = 0f;
            velocity.z = 0f;
        }
    }

    private void HandleVerticalMovement(float jetpackInput)
    {
        bool canUseJetpack = currentFillRatio > 0f && jetpackInput > 0;

        if (canUseJetpack)
        {
            UseJetpack();
        }
        else
        {
            ApplyGravity();
        }

        RefillJetpack();
    }

    private void UseJetpack()
    {
        if (jetpackTrail != null)
        {
            jetpackTrail.emitting = true;
        }
        lastTimeOfUse = Time.time;
        if (velocity.y < 0f)
        {
            velocity.y = Mathf.Lerp(velocity.y, 0f, jetpackDownwardVelocityCancelingFactor * Time.deltaTime);
        }
        velocity.y += jetpackAcceleration * Time.deltaTime;
        currentFillRatio -= Time.deltaTime / consumeDuration;
        currentFillRatio = Mathf.Clamp01(currentFillRatio);
    }

    private void ApplyGravity()
    {
        if (jetpackTrail != null)
        {
            jetpackTrail.emitting = false;
        }
        if (!isGrounded)
        {
            velocity.y += Physics.gravity.y * Time.deltaTime;
            velocity.y = Mathf.Max(velocity.y, maxFallSpeed);
        }
        else
        {
            velocity.y = groundedGravity;
        }
    }

    private void RefillJetpack()
    {
        if (Time.time - lastTimeOfUse >= refillDelay)
        {
            float refillRate = 1f / (isGrounded ? refillDurationGrounded : refillDurationInAir);
            currentFillRatio += Time.deltaTime * refillRate;
            currentFillRatio = Mathf.Clamp01(currentFillRatio);
        }
    }

    private void ApplyMovement()
    {
        characterController.Move(velocity * Time.deltaTime);
    }

    private bool IsGrounded()
    {
        Vector3 spherePosition = transform.position + Vector3.up * (characterController.radius - groundCheckRadius);
        return Physics.SphereCast(spherePosition, groundCheckRadius, Vector3.down, out RaycastHit hitInfo, groundCheckDistance, groundLayer);
    }

You’re doing a lot more physics stuff than you need to do there, probably because you’re using a CharacterController.

I recommend instead using a Rigidbody and forces.

By that I mean that in the code above, you’re manipulating velocities directly. This means that if you hit something and bounce, your code above doesn’t know or care and keeps forcing a velocity, causing it to smash again and again. I’m guessing that’s what’s happening in your jitter.

As for the joystick move cancelling fall, I’m guessing it’s something to do with the vertical velocity Lerp on line 77.

If you want to persist with the above, you will need to debug and untangle that mess, and a CharacterController isn’t really buying you anything.

A more straightforward way is to let the physics system do its job, and then use the inputs to produce forces, and apply those forces to the Rigidbody on you and let things play out by the physics system.

Usually for VR you want to lock either all the rotations to keep from getting sick, or you want to only allow rotation around the Y axis, not around X and Z. But you could do a completely-free jetpack… just be ready to get the barf bags out! :slight_smile:

I’ve made a few VR Jetpack scripts and this was the approach I used.

I also have my Jetpack Kurt game which has three or four different modes of control you can play with.

Appstore: ‎Jetpack Kurt on the App Store
GooglePlay: https://play.google.com/store/apps/details?id=com.plbm.jetpack
Android TV: https://play.google.com/store/apps/details?id=com.plbm.jetpacktv

I stripped down the essentials of Jetpack Kurt Space Flight and you can run it in a scene in my ProximityButtons project. Look in the DemoJetpackKurtSpaceFlight folder in ProximityButtons.

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons