Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question What's the best approach to implement jumping?

Discussion in 'XR Interaction Toolkit and Input' started by Devil_Inside, Nov 11, 2022.

  1. Devil_Inside

    Devil_Inside

    Joined:
    Nov 19, 2012
    Posts:
    1,117
    I'm just now exploring the locomotion system and trying to understand how it all works and I got confused about the jumping.
    If I'd like to add jumping functionality to the current locomotion system, what would be the best approach to implement it?
    1. Should I derive from one of the locomotion providers (Continuous Move Provider) and override the ComputeDesiredMove method?
    2. Should I create some kind of separate component that's solely responsible for jumping?

    What would be the best solution here?
    Thanks!
     
  2. Devil_Inside

    Devil_Inside

    Joined:
    Nov 19, 2012
    Posts:
    1,117
    Digging deeper into this, it seems that point 2. is not the right way, since Continuous Move Provider has control over the character controller and vertical velocity. If I'd be making a separate component just for jumping, I'd have to combat the gravity applied in Continuous Move Provider.

    I tried to derive from Continuous Move Provider and try to override some of the methods, but most of the fields, such as even the reference to Character Controller itself is private and I can't access.
     
  3. Devil_Inside

    Devil_Inside

    Joined:
    Nov 19, 2012
    Posts:
    1,117
    Basically I ended up copy-pasting/merging both ContinuousMoveProviderBase and ActionBasedContinuousMoveProvider into a separate script and adding jump functionality to that.
    It really feels wrong having to do that and I think that either jumping should be built-in, or the Move Providers should be more exposed for extendsion.
     
  4. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    616
    Hi there. I have jumping working. I followed this tutorial
    with a few small modifications.
     
  5. jnorth_gbc

    jnorth_gbc

    Joined:
    Nov 24, 2022
    Posts:
    3
    What modifications did you make? I am trying to follow this tutorial, but I have unlimited jumping only in one spot, and have error CS0029: Cannot implicitly convert type 'UnityEngine.Vector3' to 'float' at _collider.height = _xrRig.cameraInRigSpacePos;
     
  6. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    616
    We have a couple of differences though your issues don't seem unsolvable. I'm using an xrOrigin so it will be a bit different. I assume you're talking about the Update method? You could post it here (use a code block). Where you can jump is controlled by a layer setting that limits jumping to places marked as Teleport as I recall.
     
  7. jnorth_gbc

    jnorth_gbc

    Joined:
    Nov 24, 2022
    Posts:
    3
    I don't know how to implement XRorigin as this is my first time using Unity and I have no idea what I'm doing. Well, that's not entirely true, but this is very foreign to me.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3. using UnityEngine.XR.Interaction.Toolkit;
    4.  
    5. [RequireComponent(typeof(Rigidbody))]
    6.  
    7. public class JumpController : MonoBehaviour
    8. {
    9.     [SerializeField] private InputActionReference jumpActionReference;
    10.     [SerializeField] private float jumpForce = 500.0f;
    11.  
    12.     private XRRig _xrRig;
    13.     private CapsuleCollider _collider;
    14.     private Rigidbody _body;
    15.  
    16.     private bool isGrounded => Physics.Raycast(
    17.         new Vector2(transform.position.x, transform.position.y + 2.0f),
    18.         Vector3.down, 2.0f);
    19.  
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         _xrRig = GetComponent<XRRig>();
    25.         _collider = GetComponent<CapsuleCollider>();
    26.         _body = GetComponent<Rigidbody>();
    27.         jumpActionReference.action.performed += OnJump;
    28.     }
    29.  
    30.     private void OnJump(InputAction.CallbackContext obj) {
    31.         if (!isGrounded) return;
    32.         _body.AddForce(Vector3.up * jumpForce);
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         var center = _xrRig.cameraInRigSpacePos;
    39.         _collider.center = new Vector3(center.x, _collider.center.y, center.z);
    40.         //_collider.height = _xrRig.cameraInRigSpacePos;
    41.     }
    42. }
     
  8. jnorth_gbc

    jnorth_gbc

    Joined:
    Nov 24, 2022
    Posts:
    3
    I commented out the part that was causing the error. The implicit changing of a Vector3 to a float
     
  9. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    616
    I suspect it you have a typo on that line. cameraInRigSpacePos is in fact a Vector3 but the _collider.height is supposed to check cameraInRigSpaceHeight which is a float.