If anyone uses SteamVR and have problems with the Height calculation(Rig getting under or above the level, etc.), just don't calculate the center with the collider's Y center value but use the _xrRig.cameraInRigSpaceHeight value devided by 2, it perfectly calculates the height, clamping to min 1 also helps a lot! Here's my code: void Update() { var center = _xrRig.cameraInRigSpacePos; _collider.height = Mathf.Clamp(_xrRig.cameraInRigSpaceHeight, 1.0f, 3.0f); _collider.center = new Vector3(center.x, _collider.height / 2, center.z); }
@@mohammadrazakhan3852 So, I'm using a script I've created to handle movement and jumping and stuff like that. It's my PlayerController.cs and I have the code in the Update method, like so: // Update is called once per frame void Update() { var center = xRRig.cameraInRigSpacePos; capsuleCollider.height = Mathf.Clamp(xRRig.cameraInRigSpaceHeight, 1.0f, 3.0f); capsuleCollider.center = new Vector3(center.x, capsuleCollider.height / 2, center.z); }
Your jumping tutorial works fine as long as you're on the ground (ie. Y: 0) but you cannot jump from an elevated position. I set everything in my scene that I could climb up like ramps, platforms and so on to a "Ground" layer and changed your code to use: - [SerializeField] private LayerMask whatIsGround; [SerializeField] private Transform groundCheckPoint; ... private bool isGrounded => Physics.OverlapSphere(groundCheckPoint.position, .25f, whatIsGround).Length > 0; groundCheckPoint is just an empty child GameObject set to (0,0,0) on the XROrigin. Set whatIsGround to your "ground" layer, and you can jump from any elevation as long as what you're standing on is on the "ground" layer. Hope this helps somebody.
Thanks for the suggestion!... also found if you don't want to create a new layer and assign to objects just set .Length>1. I think the capsule collides with the overlap sphere so setting it >1 accounts for that one collision and any others it finds must be ground or something else. Thanks again!
Agreed, Valems videos are good but they are tricky for me to follow, this is perfect, functionality plus a good explanation so you learn how Unity works
@@JustinPBarnett Thanks its awesome to see youtubers who respond to their viewers, I have a small gaming channel and i try to do the same since it means a lot to everyone
Thanks so much for making this! I was struggling for a couple of days to figure out how to get specific buttons to do things in VR. You wouldn't think it would be that hard, but it was, for me!
I realy like the XR interaction toolkit, I think it works well and is pretty easy to understand, BUT it definitely doesnt work once you need to extend it to different buttons on the controllers. This video helps massively thank you
I consider myself a reasonably competent C# coder, but damn you've really got some moves. My girlfriend (a much better coder than me) was teaching me about lambdas a while ago but I hadn't gotten an opportunity to actually use one until this video. Neat stuff!
Great tutorial! Using this setup for my current project, awesome content. I was having issues with the IsGrounded function in the video always registering false for the jump, but after making a small change to it everything began working as expected. This might be due to some specific setup unique to my project, but I'll leave the change down below in case others run into something similar: private bool IsGrounded => Physics.Raycast( transform.position, Vector3.down, GetComponent().bounds.extents.y + 0.1f );
For anyone using Terrain and having trouble, bc the does not recognize it. Try using this (transform.position +new Vector3(0,0.1f,0)) position for the ray cast and make the ray 0.2f long
Great tutorials! it would be awesome to see a realistic, Boneworks like climbing system. But dont stress yourself out and keep up the very helpful tutorials.
The ones using XROrigin instead of XRRig. You have to install another package called "XR Core Utilities". Go to Package Manager-> + -> Add package by name -> copy this: com.unity.xr.core-utils Then in your script add the namespace: using Unity.XR.CoreUtils; And the methodes change a little bit, ex: using Unity.XR.CoreUtils; ... { private XROrigin _xrOrigin; private CapsuleCollider _capsuleCollider; ... void Start() { _xrOrigin = GetComponent(); } void Update() { var center = _xrOrigin.CameraInOriginSpacePos; _capsuleCollider.center = new Vector3(center.x, _capsuleCollider.height / 2, center.z); _capsuleCollider.height = Mathf.Clamp(_xrOrigin.CameraInOriginSpaceHeight, 1.4f, 1.8f); } ....
Now that Air Link is out developing with Quest 2 as your in-editor test headset is actually super easy, it's great! You just have Air Link up and can run and quit the game as easily as you would with the Rift S, big fan. I don't think that it's quite as smooth as Virtual Desktop was for actually *playing* games wirelessly, but as a development unit I'm having a much easier time with it.
Absolutely! Air Link is a game changer. (especially when I need to use my long USB-C cable to charge my light while I'm filming instead of having it plugged into the Quest 2 😅)
Again, another great tutorial. I'm using Unity 2020.3.29 and XR Rig is deprecated and it's now called XR Origin. I first started following your tutorial knowing I should switch all these Rig for Origin but towards the end, I had _collider underlined and couldn't fix it. So I thought to just change the name of my Origin in Unity and switch my code to Rig to follow yours but Visual Studio told me I couldn't do that. Now i'm stuck not being able to adjust the height of the capsule collider. The first part worked, I could jump. I also decided not to use some lines to allow pressing the A button many times and just fly. That's exactly what I wanted. But the height part is pretty cool and I'd really like to get it working.
There are some scripts you'll have to rename in order to update from xrrig to xrorigin. And the name of the variable for camera height is different now
Great video! Thanks for the tutorials. I actually just finished implementing jump this way, like almost exactly (different application of physics to the Rigidbody by specifying Force.Impulse and having a public float for gravity to mess with) only figured it out one day before you posted. This took lots of looking at outdated guides and reading new unity documentation. I was still working on figuring out the capsule collider thing and your guide saved me some extra time and headaches here. All the VR guides out there are about using the Character Controller or the old input system and didn't quite translate. There's still a bunch of stuff for me to work out with using Rigidbody and Player Controller. Collisions are a big one--Using XR Grab Interactables and running into walls that send me falling through the floor! Also was having fun with objects that transform.localScale of my player on collision, with a cooldown coroutine--but when I revert to normal size I'm falling through my ground plain. I'm hoping to avoid just using constraints because there will be things I want to knock me around later, and constraining the Y axis turns off the ability to jump. I think next steps for me is figuring out a way to handle steps/stairs/slopes without a Character Controller. Do you know if you've covered any of these or have any quick tips?
@@JustinPBarnett So theres a weird issue with jumping that I'll call the trampoline effect. Each jump is higher than the last. It doesn't matter whether you move or teleport in between or how much time you spend on the ground, the height of each consecutive jump is greater. I went back in and cleaned up my code.. Then I just switched to your exact methods even to make sure wasn't just something I'd goofed on in my own trial and error. I thought perhaps the jumpForce variable was being changed, so I did Debug.Log("Jumped with " + jumpForce); but it hasn't changed. the grounded check is working, so I'm not double jumping. The height of each jump just increases.... can you test if you are experiencing the same?
@@me_in_vr8478 oh huh, I didn't test it enough to see if that would happen. I'll look into it. You can join the discord and chat if you want! discord.gg/6mzMQx5VKk
I was using a character controller component attached to my XROrigin, but I changed it to rigidbody/capsule collider in order to do this tutorial. But one issue is that when hitting an obstacle with the XROrigin with a character controller component, my XROrigin would just stop - but when hitting an obstacle with a rigidbody/capsule collider, my XROrigin "stutters" against the obstacle rather than just stopping. Is there a fix?
I'm facing this issue to, lmk if you found a solution please This is a physics based jump movement, alot of Unity forum is divided in how it's done and most agree it's best for the user to not do so. Using the Character Controller has been great, I am actively looking for a solution that leverages it
Awesome. Thanks for doing this! Although I'm currently working on a non locomotion vr project at the moment but definitely want to know how to jump in vr. I could never get it quite right.
Hey, any chance you make a tutorial on how to make accounts and a login screen when you start up the game. Also if possible could you make a tutorial on how to make a admin panel for only people you give it to (part of the account system kinda)
RE: Character controller following camera in roomscale. This way seems nicer than overriding the CharacterControllerDriver in one of your previous videos.
Hi Justin, great video! One question: Does the new Input System require using the OpenXR backend? Or can I use it just fine with the Oculus plug-in? I ask because Unity docs say that the Oculus Integration package isn't compatible with OpenXR at this time, but I do want to use the new Input System Thank you!!
Hey Barnett, I had issues with the coding, for example tryping in serializedfield etc didn't pop up along with the majority of coding you where typing. I set-up/installed my Unity and VS coding program via another vr channels set-up tutorials (been hoping around leaning). Since everything is moving super quick with versions of these programs would it be possible to point me in the direction of the video you have for XR set-up? Can't imagine how hard must be to stay ontop of all the updates and versions coming out changing the terminology and ui of all the programs. Videos from 2 years ago you can easily see the mass amount of content updates
Super helpful! And that trick at 10:00 was awesome, didn't know that, and I'll definitely be using it from now on. I had a few issues if you don't mind helping me out. 1. Biggest one, at 13:08, there's no such thing as an XRRig for my coding, it's giving me an error. Did they change the term recently? 2. When I do the raycast grounded check, it works when I'm on a regular plane, but it doesn't work when I walk up a hill with a mesh collider. The mesh collider works great, and I walk up the hill fine, but I can't jump, because it's not returning the bool IsGrounded correctly or something. 3. Lots of VR games have up on the right stick work as jump. How would you set the button to that? I don't see anything in the list that looks like it could be that. Furthermore, what if I want left/right on the right stick to rotate, BUT if you're holding grab, then I want it to do something else (like switch equipped weapon)? 4. Also, when I move having the capsule collider working (got that working btw!) if I walk into stuff, I get huge jittering. It's like it's trying to move into it, but being pushed back, every update. It's terrible, lol. How do you fix that? 5. I'm also getting the jittering issue when I jump, where as I fly through the air everything jitters. I turned on interpolation with rigidbody, and that helped, but only if I'm only jumping, the moment I move while jumping it ruins the jump really badly, jittering, and even making me not jump as high. Extrapolate works better, but still jittery, and for some reason the wall issue is now so bad that I can jitter through walls if I keep pushing it. 6. It looks like this whole action input system is great, as you said, for having multiple types of input work with the same code (so VR, gamepad, keyboard, all with just saying "jump"). How would you set up all the other parts of having jump work with the other inputs? 7. Now that I have a capsule collider on my character, if I grab that little box, it absolutely freaks out the capsule collider, and I go flying around the world crazily. How do you make objects you grab not interact with your personal collider?
I am getting the same jittery when I jump and use continuous turn script. I asked in Discord, but no one is responding after days of it being in the dev help page :(
for the movement, how can i change the speed? I kind of want a faster paced game but a sprint button could work too. Also this tutorial worked on 2021.3!
HAHAHA I picked up the tablet from your last tutorial and since both the tablet and the player capsule have rigid bodys, I was shot out of the scene at 100 mph! Still great video keep it up!
Hey. Great video! I've got the jump to work, but for some reason the raycast returns false if i'm standing on anything but a plane. I would like to be able to jump from a box collider... What am i doing wrong? Thanks
Have you tried implementing jump mechanics only relying on the Character Controller instead of adding a rigid body on top of it? My main problem is keeping momentum while jumping
Hey Justin, how are ya? So, I was taking a look at the inputsystem docs and I couldnt really grasp a solution to this… I created a MaxForce float and wanted to gradually addforce while the button press is true and the force added since the first press is smaller or equal to the max force, but I’m not sure that’s doable with the inputsystem you used in this tutorial? I’m not really proficient in C# yet so this might be easier to implement than I’m imagining.
Hey Justin great vid. I am trying to add a jump function to my VR game but I get an error that says ‘CS0246; the type name space XRRig could not be found…’ does this mean XRRig is no longer being used in unity? Sorry I’m a super noob.
I've been trying to implement flight in XR toolkit for a couple days now (I'm a noob). This got me more than halfway there (I just didn't add any of the raycaster stuff so I can jump in the air). Can you make a video like this where you explain how to replace "Vector3.up" with a vector3 of your controllers orientation? Maybe also how to put the force in a while loop?
Scratch that. I found your swimming tutorial and implemented the reference to the camera direction as the force direction. Next step will be to make an option to increase all acceleration vector components orthogonal to the current direction of travel by a specific factor to make turning more responsive and maybe throttle the acceleration based on trigger pull extent. It's pretty rough but for anyone interested it's here: github.com/pdettinger/Unity-XR-Interaction-Toolkit/blob/main/PlayerFlight.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; [RequireComponent(typeof(Rigidbody))] public class PlayerFlight2 : MonoBehaviour { [SerializeField] InputActionReference FlyActionReference; // Set to XRI righthand/fly [SerializeField] InputActionReference AirbreakActionReference; // Set to XRI righthand/Airbreak [SerializeField] Transform TrackingReference; // Drag Your hand gameobject into this field [SerializeField] float FlyForce = 50.0f; [SerializeField] float AirbreakFactor = 5.0f; Rigidbody _body; Vector3 TurnVector; void Awake() { _body = GetComponent(); _body.constraints = RigidbodyConstraints.FreezeRotation; } void FixedUpdate() { if (FlyActionReference.action.IsPressed()) { Vector3 worldRotation = TrackingReference.TransformDirection(Vector3.forward); _body.AddForce(worldRotation * FlyForce, ForceMode.Acceleration); } if (!FlyActionReference.action.IsPressed() && AirbreakActionReference.action.IsPressed()) { Vector3 crrntVelocity = _body.velocity; _body.AddForce(crrntVelocity * -1 * AirbreakFactor, ForceMode.Acceleration); } } }
How does the event system work for cases like the joysticks where you need some axis data? I want to do a custom character controller since the provided one does not work well with other physics. The subscribed function does have information about what called it however I don't see a way to get the joystick data from there. And once we have that joystick value, how can we keep track of the orientation of the camera? I noticed that nothing changes in the editor when I move my head.
@@JustinPBarnett Yep, that works, thank you. I was confused on how to get camera orientation but I guess the main camera does rotate but it won't show its rotation changing in the editor when running.
nice video Justin! i actually own a quest 2, so im exited to see what you will make with it in the future! is there a way to test the game on rift (wich i also have) and then build it on to quest 2? Greetings from Edvard
Yes! You'll have to use the settings in this video to test on rift and then in the next (or one after?) video I'll post about how to build for the quest
Hi i want to make a Multiplayer setup , not locally, but globally using the Photon, so how can i do that ? my requirement is one person uses PC and one Person uses Quest, how to do that, Mock HMD Maybe? pl advice and guide
Hi Justin, Thank you very much for these videos. I am watching them and learn. How do I clean unity project ? I tried this tutorial. It didn't work. Every time I start my project I jump out of the project scene plane (ground). I couldn't fix this. I tried to revert and remove: I deleted the PlayerControl component, the XR jump interaction we add (and saved), rebuilt => still jumping out on startup. I deleted the *.cs file we wrote => still jumping out. cleaned the project cache (edit->properties cache) => doesn't work. Only deleting the Rigidbody and CapsuleColider I managed to get reid of the jump. I am building Android for my Oculus, Where are all the object files being saved? Why couldn't I clean this ? thanks Eyal
When i did the update code you did to update the collider i went throught the wall when my camera went through it, do you know a fix, im using 2021.3.4f1
@@JustinPBarnett wellwell, some kind of "fly to where HMD is looking at" feature would be cool. maybe in one of the next tutorials? Btw. really good tutorials, great to follow along!
Hey this tutorial is amazing but i have one problem. every time i jump and turn (with my joystick) , it crashes my game. do you know whats causing that? thanks!
The player controller script just has the script in it. I rechecked the code and I dont think our languages match if that can even happen. Can you help me out?
I had a problem, and is that when playing it on the editor It worked just fine. But when building the game and playing it (the apk file), some actions didn't work. I was doing the tutorial with another action, for example: Click the Joystick. At first, I used the default action called and it worked on the editor play mode but not the build. Then I added another binding for my specific controller (Quest2) called and worked perfectly!!!
I used to make handcontroller from your tutorial, then I combined it with this script so I could get jump and hand controller but the rigidbodies made me move so fast, how to fix it ? thank you
For quest 1, How do I adjust the following code for jumping in unity version 2021.3.10f1 with the following four errors? Code: using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.XR.Interaction.Toolkit; [RequireComponent(typeof(Rigidbody))] public class PlayerController : MonoBehaviour { [SerializeField] private InputActionReference jumpActionReference; [SerializeField] private float jumpForce = 500.0f; private XRRig _xrRig; private CapsuleCollider _collider; private Rigidbody _body; private bool IsGrounded => Physics.Raycast( new Vector2(transform.position.x, transform.position.y + 2.0f), Vector3.down, 2.0f); void Start() { _xrRig = GetComponent(); _collider = GetComponent(); _body = GetComponent(); jumpActionReference.action.performed += OnJump; } void Update() { var center = _xrRig.cameraInRigSpacePos; _collider.center = new Vector3(center.x, _collider.center.y, center.z); _collider.height = _xrRig.cameraInRigSpaceHeight; } private void OnJump(InputAction.CallbackContext obj) { if (!IsGrounded) return; _body.AddForce(Vector3.up * jumpForce); } Four errors received from using the above code: Assets\playercontroller1.cs(11,13): warning CS0618: ‘XRRig’ is obsolete: ‘XRRig has been deprecated. Use the XROrigin component instead.’ Assets\playercontroller1.cs(21,31): warning CS0618: ‘XRRig’ is obsolete: ‘XRRig has been deprecated. Use the XROrigin component instead.’ Assets\playercontroller1.cs(29,22): warning CS0618: ‘XRRig.cameraInRigSpacePos’ is obsolete: ‘cameraInRigSpacePos has been deprecated. Use the CameraInOriginSpacePos instead.’ Assets\playercontroller1.cs(31,28): warning CS0618: ‘XRRig.cameraInRigSpaceHeight’ is obsolete: ‘cameraInRigSpaceHeight has been deprecated. Use the CameraInOriginSpaceHeight instead.’
In my case the code only works if I add jumpActionReference.asset.Enable(); into the PlayerController.Start method. For reference: Unity 2020.3.19f1, Input System: 1.0.2
having this issue when I press play where it doesn't play on my Oculus Quest 1 headset and takes in no input. However, when I go back to the oculus sdk in the plugin manager everything works fine. Anyone found any solutions for this. also working on a laptop Found a half fix for unity editor where I change the OpenXR runtime in project settings but that only makes it work in the editor.
if you're still having this issue, you may notice that when he added the reference to the XR rig, it added at the top "using UnityEngine.XR.Interaction.Toolkit;" which he didn't seem to notice/mention. If you add that, it'll be fine.
I just fixed this same problem. When you start typing in XRRig there will be a green plus sign, when you click it there's a dropdown menu with the XRRig in it. I was about to sell my PC and move to Alaska. Hope this helps.
@@lint_animations4372 I was also experiencing this, I think its because both you and the object have a rigidbody. I actually dialed in the radius of my capsulecollider and made myself "skinnier" then it only happened if I brought the object close to my chest/body.
You will need to set your XR-Rig and other grabbable objects into their own separate layers. (upper right in the inspector, make new layers if non set) Then in edit->Project settings->Physics. Scroll down and uncheck the boxes where the XR-Rig/Grabbable check boxes (Whatever you call them) meet. This should mean they don't collide with each others, while still colliding with everything else.
I am only able to jump once but then I can't jump again. I put a debug log on the code to see in console if it's working and it is, it's saying I jumped every time I press the button but nothing is happening. Anyone able to help?
I don't know why but it just doesn't work for me If I press A on my oculus quest 2 controller nothing happen It's the same thing for my animation. If I press the grip button it close my hand but not how I want it and if I press the trigger button, nothing happen );
hey buddy, for some reason it is bugged, so you have to set a specific action for the oculus touch controller where you did set the xr controller action
If anyone uses SteamVR and have problems with the Height calculation(Rig getting under or above the level, etc.), just don't calculate the center with the collider's Y center value but use the _xrRig.cameraInRigSpaceHeight value devided by 2, it perfectly calculates the height, clamping to min 1 also helps a lot!
Here's my code:
void Update()
{
var center = _xrRig.cameraInRigSpacePos;
_collider.height = Mathf.Clamp(_xrRig.cameraInRigSpaceHeight, 1.0f, 3.0f);
_collider.center = new Vector3(center.x, _collider.height / 2, center.z);
}
Thanks so much for finding this Nickk888!
Great tip Nick! Settled some of my falling through the floor problems. Not using SteamVR, but Oculus Quest/Link and it worked for me.
@@me_in_vr8478 good to know it also works on other devices! Thanks for the info!
@@me_in_vr8478 hey my player is also falling from the scene so what should I DO ? WHERE should I paste this code ??
@@mohammadrazakhan3852 So, I'm using a script I've created to handle movement and jumping and stuff like that. It's my PlayerController.cs and I have the code in the Update method, like so:
// Update is called once per frame
void Update()
{
var center = xRRig.cameraInRigSpacePos;
capsuleCollider.height = Mathf.Clamp(xRRig.cameraInRigSpaceHeight, 1.0f, 3.0f);
capsuleCollider.center = new Vector3(center.x, capsuleCollider.height / 2, center.z);
}
I have 20+ years of programming experience and this is all so confusing to me. Thank you for taking the time to make this very detailed tutorial.
You’re welcome!
Your jumping tutorial works fine as long as you're on the ground (ie. Y: 0) but you cannot jump from an elevated position. I set everything in my scene that I could climb up like ramps, platforms and so on to a "Ground" layer and changed your code to use: -
[SerializeField] private LayerMask whatIsGround;
[SerializeField] private Transform groundCheckPoint;
...
private bool isGrounded => Physics.OverlapSphere(groundCheckPoint.position, .25f, whatIsGround).Length > 0;
groundCheckPoint is just an empty child GameObject set to (0,0,0) on the XROrigin. Set whatIsGround to your "ground" layer, and you can jump from any elevation as long as what you're standing on is on the "ground" layer. Hope this helps somebody.
Thanks for the suggestion!... also found if you don't want to create a new layer and assign to objects just set .Length>1. I think the capsule collides with the overlap sphere so setting it >1 accounts for that one collision and any others it finds must be ground or something else. Thanks again!
Thanks very much for sharing this. It helped me!
Thank you!
this helped so much as i am making a spiderman, no, arachnid person, game.
Download the Project (for FREE): www.vrcreators.io/codedownloads
Short, sweet, and to the point, will never unsub
many thanks! 🙏
Agreed, Valems videos are good but they are tricky for me to follow, this is perfect, functionality plus a good explanation so you learn how Unity works
@@cleverballoon6535 Appreciate you!!
@@JustinPBarnett Thanks its awesome to see youtubers who respond to their viewers, I have a small gaming channel and i try to do the same since it means a lot to everyone
@@cleverballoon6535 sure thing, yea I love interacting with people in the comments. Good luck with your channel!!
Thanks so much for making this! I was struggling for a couple of days to figure out how to get specific buttons to do things in VR. You wouldn't think it would be that hard, but it was, for me!
You're so welcome! Yea this VR stuff is a bit complicated at first.
I realy like the XR interaction toolkit, I think it works well and is pretty easy to understand, BUT it definitely doesnt work once you need to extend it to different buttons on the controllers. This video helps massively thank you
I consider myself a reasonably competent C# coder, but damn you've really got some moves. My girlfriend (a much better coder than me) was teaching me about lambdas a while ago but I hadn't gotten an opportunity to actually use one until this video. Neat stuff!
Omg they're WONDERFUL once you get the hang of them. Saves so much time!
Great tutorial! Using this setup for my current project, awesome content. I was having issues with the IsGrounded function in the video always registering false for the jump, but after making a small change to it everything began working as expected. This might be due to some specific setup unique to my project, but I'll leave the change down below in case others run into something similar:
private bool IsGrounded => Physics.Raycast(
transform.position, Vector3.down, GetComponent().bounds.extents.y + 0.1f
);
For anyone using Terrain and having trouble, bc the does not recognize it. Try using this (transform.position +new Vector3(0,0.1f,0)) position for the ray cast and make the ray 0.2f long
You're a lifesaver my guy! I've been freaking out for days on how I can get this to work
Great tutorials! it would be awesome to see a realistic, Boneworks like climbing system. But dont stress yourself out and keep up the very helpful tutorials.
Great suggestion! I'd love to do that
The ones using XROrigin instead of XRRig. You have to install another package called "XR Core Utilities".
Go to Package Manager-> + -> Add package by name -> copy this: com.unity.xr.core-utils
Then in your script add the namespace: using Unity.XR.CoreUtils;
And the methodes change a little bit, ex:
using Unity.XR.CoreUtils;
...
{
private XROrigin _xrOrigin;
private CapsuleCollider _capsuleCollider;
...
void Start()
{
_xrOrigin = GetComponent();
}
void Update()
{
var center = _xrOrigin.CameraInOriginSpacePos;
_capsuleCollider.center = new Vector3(center.x, _capsuleCollider.height / 2, center.z);
_capsuleCollider.height = Mathf.Clamp(_xrOrigin.CameraInOriginSpaceHeight, 1.4f, 1.8f);
}
....
you save me thank
could you please, explain how to do this. Nothing is working for me
Thank you!!
Now that Air Link is out developing with Quest 2 as your in-editor test headset is actually super easy, it's great! You just have Air Link up and can run and quit the game as easily as you would with the Rift S, big fan. I don't think that it's quite as smooth as Virtual Desktop was for actually *playing* games wirelessly, but as a development unit I'm having a much easier time with it.
Absolutely! Air Link is a game changer. (especially when I need to use my long USB-C cable to charge my light while I'm filming instead of having it plugged into the Quest 2 😅)
Again, another great tutorial. I'm using Unity 2020.3.29 and XR Rig is deprecated and it's now called XR Origin. I first started following your tutorial knowing I should switch all these Rig for Origin but towards the end, I had _collider underlined and couldn't fix it. So I thought to just change the name of my Origin in Unity and switch my code to Rig to follow yours but Visual Studio told me I couldn't do that. Now i'm stuck not being able to adjust the height of the capsule collider. The first part worked, I could jump. I also decided not to use some lines to allow pressing the A button many times and just fly. That's exactly what I wanted. But the height part is pretty cool and I'd really like to get it working.
There are some scripts you'll have to rename in order to update from xrrig to xrorigin. And the name of the variable for camera height is different now
Great video! Thanks for the tutorials. I actually just finished implementing jump this way, like almost exactly (different application of physics to the Rigidbody by specifying Force.Impulse and having a public float for gravity to mess with) only figured it out one day before you posted. This took lots of looking at outdated guides and reading new unity documentation. I was still working on figuring out the capsule collider thing and your guide saved me some extra time and headaches here. All the VR guides out there are about using the Character Controller or the old input system and didn't quite translate.
There's still a bunch of stuff for me to work out with using Rigidbody and Player Controller. Collisions are a big one--Using XR Grab Interactables and running into walls that send me falling through the floor! Also was having fun with objects that transform.localScale of my player on collision, with a cooldown coroutine--but when I revert to normal size I'm falling through my ground plain. I'm hoping to avoid just using constraints because there will be things I want to knock me around later, and constraining the Y axis turns off the ability to jump. I think next steps for me is figuring out a way to handle steps/stairs/slopes without a Character Controller. Do you know if you've covered any of these or have any quick tips?
I don't think I've covered those topics, but 100% will be getting to most if not all of them in the near future!
Thanks so much for your feedback!!
@@JustinPBarnett So theres a weird issue with jumping that I'll call the trampoline effect. Each jump is higher than the last. It doesn't matter whether you move or teleport in between or how much time you spend on the ground, the height of each consecutive jump is greater.
I went back in and cleaned up my code.. Then I just switched to your exact methods even to make sure wasn't just something I'd goofed on in my own trial and error. I thought perhaps the jumpForce variable was being changed, so I did Debug.Log("Jumped with " + jumpForce); but it hasn't changed. the grounded check is working, so I'm not double jumping. The height of each jump just increases.... can you test if you are experiencing the same?
@@me_in_vr8478 oh huh, I didn't test it enough to see if that would happen. I'll look into it. You can join the discord and chat if you want! discord.gg/6mzMQx5VKk
I was using a character controller component attached to my XROrigin, but I changed it to rigidbody/capsule collider in order to do this tutorial. But one issue is that when hitting an obstacle with the XROrigin with a character controller component, my XROrigin would just stop - but when hitting an obstacle with a rigidbody/capsule collider, my XROrigin "stutters" against the obstacle rather than just stopping. Is there a fix?
I'm facing this issue to, lmk if you found a solution please
This is a physics based jump movement, alot of Unity forum is divided in how it's done and most agree it's best for the user to not do so. Using the Character Controller has been great, I am actively looking for a solution that leverages it
Man your new space looks awesome, your thumnnais are catchy and your content rocks!! 😁
Thanks a ton!
Awesome. Thanks for doing this! Although I'm currently working on a non locomotion vr project at the moment but definitely want to know how to jump in vr. I could never get it quite right.
Sure thing! You'll do a locomotion project eventually I'm sure!
Hey, any chance you make a tutorial on how to make accounts and a login screen when you start up the game. Also if possible could you make a tutorial on how to make a admin panel for only people you give it to (part of the account system kinda)
Yea I can look into that!
Dude, you are absolutely awesome. Thank you very much! I really do hope your Patreon takes off - I hope for many new game tutorials there :)
Thanks so much! And I hope so too 😅, regardless there's lots more tutorials to come!
Great tutorial! My only problem right now is maintaining momentum and control mid-air is there something I can add to the existing script to fix this?
RE: Character controller following camera in roomscale. This way seems nicer than overriding the CharacterControllerDriver in one of your previous videos.
Totally agree! I'm getting better as I learn too!
Hi Justin, great video! One question: Does the new Input System require using the OpenXR backend? Or can I use it just fine with the Oculus plug-in?
I ask because Unity docs say that the Oculus Integration package isn't compatible with OpenXR at this time, but I do want to use the new Input System
Thank you!!
your next video should explain how to set up hand tracking. For example something that can be used with the quest
Definitely gonna try to do that pretty soon now that I’ve got the quest 2!
Hey Barnett, I had issues with the coding, for example tryping in serializedfield etc didn't pop up along with the majority of coding you where typing.
I set-up/installed my Unity and VS coding program via another vr channels set-up tutorials (been hoping around leaning).
Since everything is moving super quick with versions of these programs would it be possible to point me in the direction of the video you have for XR set-up?
Can't imagine how hard must be to stay ontop of all the updates and versions coming out changing the terminology and ui of all the programs. Videos from 2 years ago you can easily see the mass amount of content updates
Great tutorial. As always :)
And if you are looking for a new tutorial idea: How about a 3rd person controller? Similar to MOSS or ASTRO BOT ...
Thanks! And cool idea! I'd love to give that a shot!
thanks, I've been looking for a tutorial like this. :)
No problem!
Super helpful! And that trick at 10:00 was awesome, didn't know that, and I'll definitely be using it from now on.
I had a few issues if you don't mind helping me out.
1. Biggest one, at 13:08, there's no such thing as an XRRig for my coding, it's giving me an error. Did they change the term recently?
2. When I do the raycast grounded check, it works when I'm on a regular plane, but it doesn't work when I walk up a hill with a mesh collider. The mesh collider works great, and I walk up the hill fine, but I can't jump, because it's not returning the bool IsGrounded correctly or something.
3. Lots of VR games have up on the right stick work as jump. How would you set the button to that? I don't see anything in the list that looks like it could be that. Furthermore, what if I want left/right on the right stick to rotate, BUT if you're holding grab, then I want it to do something else (like switch equipped weapon)?
4. Also, when I move having the capsule collider working (got that working btw!) if I walk into stuff, I get huge jittering. It's like it's trying to move into it, but being pushed back, every update. It's terrible, lol. How do you fix that?
5. I'm also getting the jittering issue when I jump, where as I fly through the air everything jitters. I turned on interpolation with rigidbody, and that helped, but only if I'm only jumping, the moment I move while jumping it ruins the jump really badly, jittering, and even making me not jump as high. Extrapolate works better, but still jittery, and for some reason the wall issue is now so bad that I can jitter through walls if I keep pushing it.
6. It looks like this whole action input system is great, as you said, for having multiple types of input work with the same code (so VR, gamepad, keyboard, all with just saying "jump"). How would you set up all the other parts of having jump work with the other inputs?
7. Now that I have a capsule collider on my character, if I grab that little box, it absolutely freaks out the capsule collider, and I go flying around the world crazily. How do you make objects you grab not interact with your personal collider?
Join the Discord and we can help you out with your questions!
I am getting the same jittery when I jump and use continuous turn script. I asked in Discord, but no one is responding after days of it being in the dev help page
:(
for the movement, how can i change the speed? I kind of want a faster paced game but a sprint button could work too. Also this tutorial worked on 2021.3!
HAHAHA I picked up the tablet from your last tutorial and since both the tablet and the player capsule have rigid bodys, I was shot out of the scene at 100 mph! Still great video keep it up!
OMG 😂
Hey. Great video! I've got the jump to work, but for some reason the raycast returns false if i'm standing on anything but a plane. I would like to be able to jump from a box collider... What am i doing wrong? Thanks
Maybe try extending the raycast a bit more or making sure the other ground objects are being recognized in the raycast layermask
Have you tried implementing jump mechanics only relying on the Character Controller instead of adding a rigid body on top of it? My main problem is keeping momentum while jumping
Personally, I like to keep as much stuff physics-based as I can in my games
I was just looking into this and suddenly this tutorial appears, giving me all the information I didn't know I needed...GET OUT OF MY HEAD 🤣
Hahaha, glad I could help!
Hey Justin, how are ya?
So, I was taking a look at the inputsystem docs and I couldnt really grasp a solution to this… I created a MaxForce float and wanted to gradually addforce while the button press is true and the force added since the first press is smaller or equal to the max force, but I’m not sure that’s doable with the inputsystem you used in this tutorial? I’m not really proficient in C# yet so this might be easier to implement than I’m imagining.
Basically a jedi jump, the longer you hold, higher it goes
For that you'd probably have to check in the update method every frame if the button is being pressed. If so, just increase the force a little bit.
Hey Justin great vid. I am trying to add a jump function to my VR game but I get an error that says ‘CS0246; the type name space XRRig could not be found…’ does this mean XRRig is no longer being used in unity? Sorry I’m a super noob.
Oh they updated it to “XROrigin”, try that one!
I've been trying to implement flight in XR toolkit for a couple days now (I'm a noob). This got me more than halfway there (I just didn't add any of the raycaster stuff so I can jump in the air). Can you make a video like this where you explain how to replace "Vector3.up" with a vector3 of your controllers orientation? Maybe also how to put the force in a while loop?
Scratch that. I found your swimming tutorial and implemented the reference to the camera direction as the force direction. Next step will be to make an option to increase all acceleration vector components orthogonal to the current direction of travel by a specific factor to make turning more responsive and maybe throttle the acceleration based on trigger pull extent. It's pretty rough but for anyone interested it's here:
github.com/pdettinger/Unity-XR-Interaction-Toolkit/blob/main/PlayerFlight.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(Rigidbody))]
public class PlayerFlight2 : MonoBehaviour
{
[SerializeField] InputActionReference FlyActionReference; // Set to XRI righthand/fly
[SerializeField] InputActionReference AirbreakActionReference; // Set to XRI righthand/Airbreak
[SerializeField] Transform TrackingReference; // Drag Your hand gameobject into this field
[SerializeField] float FlyForce = 50.0f;
[SerializeField] float AirbreakFactor = 5.0f;
Rigidbody _body;
Vector3 TurnVector;
void Awake()
{
_body = GetComponent();
_body.constraints = RigidbodyConstraints.FreezeRotation;
}
void FixedUpdate()
{
if (FlyActionReference.action.IsPressed())
{
Vector3 worldRotation = TrackingReference.TransformDirection(Vector3.forward);
_body.AddForce(worldRotation * FlyForce, ForceMode.Acceleration);
}
if (!FlyActionReference.action.IsPressed() && AirbreakActionReference.action.IsPressed())
{
Vector3 crrntVelocity = _body.velocity;
_body.AddForce(crrntVelocity * -1 * AirbreakFactor, ForceMode.Acceleration);
}
}
}
How does the event system work for cases like the joysticks where you need some axis data? I want to do a custom character controller since the provided one does not work well with other physics. The subscribed function does have information about what called it however I don't see a way to get the joystick data from there. And once we have that joystick value, how can we keep track of the orientation of the camera? I noticed that nothing changes in the editor when I move my head.
All you need is to read the Vector2 or Vector3 value once that action is performed using value = reference.action.ReadValue();
@@JustinPBarnett Yep, that works, thank you. I was confused on how to get camera orientation but I guess the main camera does rotate but it won't show its rotation changing in the editor when running.
nice video Justin! i actually own a quest 2, so im exited to see what you will make with it in the future! is there a way to test the game on rift (wich i also have) and then build it on to quest 2? Greetings from Edvard
Yes! You'll have to use the settings in this video to test on rift and then in the next (or one after?) video I'll post about how to build for the quest
Hi i want to make a Multiplayer setup , not locally, but globally using the Photon, so how can i do that ? my requirement is one person uses PC and one Person uses Quest, how to do that, Mock HMD Maybe? pl advice and guide
Awesome tutorial! Loved it but when jumping the scene goes all jittery (jumping and falling) know a fix?
Join the discord if you're still having issues and we can help you troubleshoot!
Hi Justin,
Thank you very much for these videos. I am watching them and learn.
How do I clean unity project ?
I tried this tutorial. It didn't work. Every time I start my project I jump out of the project scene plane (ground). I couldn't fix this.
I tried to revert and remove:
I deleted the PlayerControl component, the XR jump interaction we add (and saved), rebuilt => still jumping out on startup.
I deleted the *.cs file we wrote => still jumping out.
cleaned the project cache (edit->properties cache) => doesn't work.
Only deleting the Rigidbody and CapsuleColider I managed to get reid of the jump.
I am building Android for my Oculus, Where are all the object files being saved?
Why couldn't I clean this ?
thanks
Eyal
Join the discord if you're still having issues and we can help you troubleshoot!
Thank you so much!
You’re so welcome!
Update function gives me this Error: NullReferenceException: Object reference not set to an instance of an object
have i done something wrong
When i did the update code you did to update the collider i went throught the wall when my camera went through it, do you know a fix, im using 2021.3.4f1
Hey, I want to move with my left joystick of my Oculus. How can I set it on Input actions menu?
Check out my "How to Move" tutorial!
if we leave away the variable "IsGrounded", this script could be used for flying?
you could jump infinite times!! so yea! lol
@@JustinPBarnett wellwell, some kind of "fly to where HMD is looking at" feature would be cool. maybe in one of the next tutorials? Btw. really good tutorials, great to follow along!
Hey this tutorial is amazing but i have one problem. every time i jump and turn (with my joystick) , it crashes my game. do you know whats causing that? thanks!
Oh that's very odd... I'll look into it
The player controller script just has the script in it. I rechecked the code and I dont think our languages match if that can even happen. Can you help me out?
What language are you using?? Unity only uses C#
I had a problem, and is that when playing it on the editor It worked just fine. But when building the game and playing it (the apk file), some actions didn't work.
I was doing the tutorial with another action, for example: Click the Joystick. At first, I used the default action called and it worked on the editor play mode but not the build. Then I added another binding for my specific controller (Quest2) called and worked perfectly!!!
I did everything you do in the video but I go through walls so collision is not working properly (I tried to set it to Continuous) What should I do?
Join the discord and we can help you troubleshoot!
I used to make handcontroller from your tutorial, then I combined it with this script so I could get jump and hand controller but the rigidbodies made me move so fast, how to fix it ? thank you
Join the discord and we can help you troubleshoot!
Great Tutorial. Can you please do a Tutorial for Google Cardboard in version 2021.2 Beta. Thanks :)
Ehhhh doubt it 😬 Google cardboard is getting phased out
This doesnt work. I've run through it multiple times now. Sadly doesnt work anymore idk why.
Join the discord and we can help you troubleshoot!
bro in unity i can't see that file that name was XRI LeftHand/jump ,kindly help me bro
For quest 1, How do I adjust the following code for jumping in unity version 2021.3.10f1 with the following four errors?
Code:
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.XR.Interaction.Toolkit;
[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour
{
[SerializeField] private InputActionReference jumpActionReference;
[SerializeField] private float jumpForce = 500.0f;
private XRRig _xrRig;
private CapsuleCollider _collider;
private Rigidbody _body;
private bool IsGrounded => Physics.Raycast(
new Vector2(transform.position.x, transform.position.y + 2.0f),
Vector3.down, 2.0f);
void Start()
{
_xrRig = GetComponent();
_collider = GetComponent();
_body = GetComponent();
jumpActionReference.action.performed += OnJump;
}
void Update()
{
var center = _xrRig.cameraInRigSpacePos;
_collider.center = new Vector3(center.x, _collider.center.y, center.z);
_collider.height = _xrRig.cameraInRigSpaceHeight;
}
private void OnJump(InputAction.CallbackContext obj)
{
if (!IsGrounded) return;
_body.AddForce(Vector3.up * jumpForce);
}
Four errors received from using the above code:
Assets\playercontroller1.cs(11,13): warning CS0618: ‘XRRig’ is obsolete: ‘XRRig has been deprecated. Use the XROrigin component instead.’
Assets\playercontroller1.cs(21,31): warning CS0618: ‘XRRig’ is obsolete: ‘XRRig has been deprecated. Use the XROrigin component instead.’
Assets\playercontroller1.cs(29,22): warning CS0618: ‘XRRig.cameraInRigSpacePos’ is obsolete: ‘cameraInRigSpacePos has been deprecated. Use the CameraInOriginSpacePos instead.’
Assets\playercontroller1.cs(31,28): warning CS0618: ‘XRRig.cameraInRigSpaceHeight’ is obsolete: ‘cameraInRigSpaceHeight has been deprecated. Use the CameraInOriginSpaceHeight instead.’
In my case the code only works if I add jumpActionReference.asset.Enable(); into the PlayerController.Start method. For reference: Unity 2020.3.19f1, Input System: 1.0.2
Yes you’ll need to enable the asset first
My code just doesn't work, I've tried every way I need help
Maybe sending the code (I cant pay for patreon due to card being suspended)
Join the discord and we can help you troubleshoot!
having this issue when I press play where it doesn't play on my Oculus Quest 1 headset and takes in no input. However, when I go back to the oculus sdk in the plugin manager everything works fine. Anyone found any solutions for this. also working on a laptop
Found a half fix for unity editor where I change the OpenXR runtime in project settings but that only makes it work in the editor.
I'm actually posting a video on Wednesday about how to build and test to the Quest!
What programm are you using to edit your code? I have problems with mine.
JetBrains Rider. Highly recommend 👌
@@JustinPBarnett Thanks
@@loeweguckmal Sure thing! It's free for students
hi my unity console keep getting an error (NullReferenceException: Object reference not set to an instance of an object) CS:32
My code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.XR.Interaction.Toolkit;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
public class XRPlayerController : MonoBehaviour
{
[SerializeField] private InputActionReference JumpActionReference;
[SerializeField] private float JumpForce = 500.0f;
private Rigidbody _body;
private XRRig _xrRig;
private CapsuleCollider _collider;
private bool IsGrounded => Physics.Raycast(
new Vector2(transform.position.x, transform.position.y + 2.0f), Vector3.down, 2.0f);
void Start()
{
_body = GetComponent();
_collider = GetComponent();
_xrRig = GetComponent();
JumpActionReference.action.performed += OnJump;
}
void Update()
{
var center = _xrRig.cameraInRigSpacePos;
_collider.height = Mathf.Clamp(_xrRig.cameraInRigSpaceHeight, 1.0f, 3.0f);
_collider.center = new Vector3(center.x, _collider.height / 2, center.z);
}
private void OnJump(InputAction.CallbackContext obj)
{
if (!IsGrounded) return;
_body.AddForce(Vector3.up * JumpForce);
}
}
Join the discord and we can help you troubleshoot!
hi i have tried to make my vr player jump for 11 houers and i cant get it to work can anyone help me
Join the discord and we'll help you out!
Is this the same with 2021 install
Can you put the code in the description?
There's a link in the description you have get it at!
Hi i followed your video for the jump and now when i play my game i fly in the sky without pressing any buttons
what IDLE are you using?
Jetbrains Rider
thanks :D
You're welcome!
I wrote the code the exact same way you did, but an error popped up and say that it couldn't find "XRRig". Any ideas on how I can fix this?
Sounds like you missed setting the XRRig either in your inspector or the start method
if you're still having this issue, you may notice that when he added the reference to the XR rig, it added at the top "using UnityEngine.XR.Interaction.Toolkit;" which he didn't seem to notice/mention. If you add that, it'll be fine.
I just fixed this same problem. When you start typing in XRRig there will be a green plus sign, when you click it there's a dropdown menu with the XRRig in it. I was about to sell my PC and move to Alaska. Hope this helps.
@@jbg0801 umm no its now called XrOrigin instead of XrRig
@@tacticalmythic7945 if that's changed in the FIVE MONTHS since I wrote that message, so be it
I have a problem with grabbing objects, when i grab them they act like a jet pack and psh me in the opposite direction
Try checking out my physics grab tutorial! The default xr interaction grab doesn't really work too well
@@JustinPBarnett thankyou sooo much i really thought you would never reply!
@@lint_animations4372 I was also experiencing this, I think its because both you and the object have a rigidbody. I actually dialed in the radius of my capsulecollider and made myself "skinnier" then it only happened if I brought the object close to my chest/body.
You will need to set your XR-Rig and other grabbable objects into their own separate layers. (upper right in the inspector, make new layers if non set) Then in edit->Project settings->Physics. Scroll down and uncheck the boxes where the XR-Rig/Grabbable check boxes (Whatever you call them) meet. This should mean they don't collide with each others, while still colliding with everything else.
@@pegsGamesVR I have the same issue. Tried everything - not working. I'm still flying away while grab and object :(
I will literally pay for you to help me with my project at this point tbh
You can download the code for the project to get it working!
I am only able to jump once but then I can't jump again. I put a debug log on the code to see in console if it's working and it is, it's saying I jumped every time I press the button but nothing is happening. Anyone able to help?
Now there's nothing happening at all. Not even in the debug log and I started from scratch and followed the tutorial from the start.
I don't know why but it just doesn't work for me
If I press A on my oculus quest 2 controller nothing happen
It's the same thing for my animation. If I press the grip button it close my hand but not how I want it and if I press the trigger button, nothing happen );
hey buddy, for some reason it is bugged, so you have to set a specific action for the oculus touch controller where you did set the xr controller action
can u just add the script in the desription in typed it and it didnt work
Sadly did not work at all for me, not sure what I'm doing wrong
_collider.center = new Vector3(center.x, _xrRig.cameraInRigSpaceHeight / 2, center.z); works better ;D
Thanks!
at 12:26 he looks high
Only if you pause it
hey I have defualt inputs only some are there do I need to add all the ones you got @Jusrtin P Barnett