Search Unity

Question Looking for a reliable way to implement jump

Discussion in 'XR Interaction Toolkit and Input' started by PublicEnumE, Apr 5, 2021.

  1. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    I'm not finding a built-in way to add a physics jump to the character controller, using the XR Interaction Toolkit.

    Am I missing one, or have people been implementing their own? And if so - is there a common approach used by this community?

    Many thanks. :)
     
  2. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    No responses today. I’m guessing that either...

    A. There actually isn’t a standard way to do this yet.

    B. This question is so old, boring, or frequently asked that no one wants to touch it.

    if it’s one or the other, please give me a heads up. I haven’t had much luck finding previous forum posts on the subject.

    Ty!
     
  3. Cloudwalker_

    Cloudwalker_

    Joined:
    Jan 3, 2014
    Posts:
    140
    Because physics in VR is a very difficult thing to get right and most people don't try. Those that do are making their own games and keeping the tech to themselves. I publish a rigid body player built to mimic the Boneworks player. Spent 2 months working on it to get it to a usable state.

    The character controller does not use physics because it is not a rigid body, you would have to program jumping, gravity, and momentum yourself to "fake physics"

    https://assetstore.unity.com/packages/tools/physics/hexabody-vr-player-controller-185521
     
    PublicEnumE likes this.
  4. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    Thank you for your reply. Your controller package looks great.

    Honestly, I'm a bit surprised to hear this. I would have expected a jump to be built in to the XR Interaction Toolkit, since it provides components for continuous movement, turning, snap turning, teleporting, and other locomotion controls. Jumping is a common action in VR, and is right in line with those.

    Does anyone know if jump support is a planned part of XR Interaction Toolkit, in the future?
     
  5. dpcactus

    dpcactus

    Joined:
    Jan 13, 2020
    Posts:
    53
    It's not. I wrote my own jumping / movement because of that, however, I do not use physics for that and only the pain character controller. Also still use the device based XR Rig and not the Action based one.

    Maybe this gives you an idea how to do it. (Do not take this as a professional solution, because I'm certainly not a pro at this! It simply is one!)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5. using UnityEngine.XR.Interaction.Toolkit;
    6.  
    7. public class XR_JumpController : MonoBehaviour
    8. {
    9.     public XRNode inputSource;
    10.     private InputDevice device;
    11.     public AudioClip jumpSound;
    12.     public  LayerMask CeilingLayer;
    13.     public float jumpHeight = 5.0F;
    14.     public  bool isJumping = false;
    15.     private bool lastState = false;
    16.     private float jumppos;
    17.     public float jumpSpeed = 4.0f;
    18.  
    19.     private float fallingSpeed;
    20.     private XRRig rig;
    21.     private AudioSource MainSFX;
    22.     private Vector2 inputAxis;
    23.     private CharacterController character;
    24.        
    25.     void Start()
    26.     {
    27.         character = GetComponent<CharacterController>();    
    28.         rig = GetComponent<XRRig>();
    29.         MainSFX = GetComponent<AudioSource>();
    30.         MainSFX.clip = jumpSound;
    31.         InitController();
    32.     }
    33.  
    34.     void InitController()
    35.     {
    36.         device = InputDevices.GetDeviceAtXRNode(inputSource);
    37.     }
    38.  
    39.  
    40.     // Update is called once per frame
    41.     void Update()
    42.     {
    43.         if (!device.isValid)
    44.             InitController();
    45.  
    46.         if (SecondaryButtonDown() & this.gameObject.GetComponent<XRMovement>().CheckIfGrounded())
    47.         {
    48.             isJumping = true;
    49.             MainSFX.Play();
    50.             jumppos = character.transform.position.y;        
    51.             character.slopeLimit = 90; // fixes the jump directly in fornt of thing while pressing forward. has to be reset to 45 when isJumping = false
    52.         }
    53.  
    54.         if (isJumping)
    55.             Jump();
    56.  
    57.         if (HitCeiling())
    58.             isJumping = false;
    59.  
    60.  
    61.     }
    62.  
    63.     public bool HitCeiling()
    64.     {
    65.         Vector3 rayStart = transform.TransformPoint(character.center);
    66.         float rayLength = character.center.y + 0.1f;
    67.         bool hasHit = Physics.SphereCast(rayStart, character.radius, Vector3.up, out RaycastHit hitInfo, rayLength, CeilingLayer);
    68.         return hasHit;
    69.     }
    70.  
    71.  
    72.  
    73.     private void Jump()
    74.     {
    75.     if (character.transform.position.y >= jumppos + jumpHeight)
    76.         {
    77.             isJumping = false;
    78.             character.slopeLimit = 45;
    79.         }
    80.     character.Move(Vector3.up * jumpSpeed * Time.smoothDeltaTime);
    81.     }
    82.  
    83.     public bool SecondaryButtonDown()
    84.     {    
    85.         if (device.TryGetFeatureValue(CommonUsages.secondaryButton, out bool secondaryButtonValue) && secondaryButtonValue)
    86.         {
    87.             bool tempStatePrimary = secondaryButtonValue;
    88.  
    89.             if (tempStatePrimary != lastState)  //Button Down
    90.             {
    91.                 lastState = tempStatePrimary;
    92.                 return true;            
    93.             }
    94.         }
    95.         else
    96.         {
    97.             lastState = false;
    98.         }
    99.         return false;
    100.     }
    101.  
    102. }
     
    Last edited: Apr 6, 2021
    cmcosta81 and PublicEnumE like this.
  6. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    Thank you very much for that suggestion. I’ll use it as a guide.

    Huh - You’re right! I went through the list of VR games I thought had a jump, but very few of them did. I could have sworn Population One had a jump, but nope!

    And apparently for good reason - it’s clear that would make some people nauseous.

    I should probably reconsider if it’s worth having at all. Maybe a tall step height would work just as well.

    Thanks!
     
  7. MarkSharpe

    MarkSharpe

    Joined:
    Feb 3, 2021
    Posts:
    27
    Has anyone any insight on how to increase the xr rig locomotion speed for a short duration using a trigger event?
     
  8. jjclose

    jjclose

    Joined:
    Feb 7, 2017
    Posts:
    1
    I implemented something that may be helpful for this. but you may have had your question answered since then -- let me know and if not I'm happy to provide details.