Created
May 14, 2022 20:27
-
-
Save matthewholliday/ee69dcaa842e10b4df5c52bde098a82e to your computer and use it in GitHub Desktop.
Player controller for jumping in Unity XR (should be attached to XR Origin.)
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.InputSystem; | |
public class JumpController : MonoBehaviour | |
{ | |
[SerializeField] private InputActionReference jumpActionReference; | |
[SerializeField] private float jumpForce = 500.0f; | |
private Rigidbody _body; | |
private bool IsGrounded => Physics.Raycast( | |
new Vector2(transform.position.magnitude, transform.position.y + 2.0f), | |
Vector3.down, | |
2.0f | |
); | |
// Start is called before the first frame update | |
void Start() | |
{ | |
_body = GetComponent<Rigidbody>(); | |
jumpActionReference.action.performed += OnJump; | |
} | |
private void OnJump(InputAction.CallbackContext obj) | |
{ | |
if (!IsGrounded) return; | |
_body.AddForce(Vector3.up * jumpForce); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment