Skip to content

Instantly share code, notes, and snippets.

@matthewholliday
Created May 14, 2022 20:27
Show Gist options
  • Save matthewholliday/ee69dcaa842e10b4df5c52bde098a82e to your computer and use it in GitHub Desktop.
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.)
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