How to Make Physics-Based Hands in Unity XR

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 2. 07. 2024
  • The most critical but HARDEST part of VR is getting the hands right. If you miss the mark here, you might as well make an FPS game...
    Hey, devs! We're continuing our series on how to make a VR game, and this week we're talking about physics-based hands! That's right! No more phasing through walls and picking up objects from the middle. Take your game to the next level with these tricks!
    đŸ“ș Watch Next: ‱ What Do I Need to Beco...
    đŸ„œ Enroll in the (now FREE) VR Creator Academy: skool.com/vrcreatoracademy
    ⌚ Timestamps
    0:00 Subscribe!
    0:37 Settings & Setup
    1:39 Add Colliders
    5:24 Add Rigidbody
    6:06 Add Direct Interactor
    7:22 Code!
    10:43 Position Tracking Test
    11:51 MORE Code!
    13:51 Position + Rotation Test
    14:06 Adding Offsets
    15:51 One more major issue...
    16:33 Like!
    17:04 Outro

Komentáƙe • 136

  • @Piepop101
    @Piepop101 Pƙed 2 lety +25

    For anyone who has issues with hands randomly flipping 180 degrees or flipping when turning the hand(Over-turning), this is the fix for the script(Adds checks for rotation):
    private void PhysicsMove()
    {
    // Position
    var positionWithOffset = _followTarget.TransformPoint(positionOffset);
    var distance = Vector3.Distance(positionWithOffset, transform.position);
    _body.velocity = (positionWithOffset - transform.position).normalized * (followSpeed * distance);
    // Rotation
    var rotationWithOffset = _followTarget.rotation * Quaternion.Euler(rotationOffset);
    var q = rotationWithOffset * Quaternion.Inverse(_body.rotation);
    q.ToAngleAxis(out float angle, out Vector3 axis);
    if (Mathf.Abs(axis.magnitude) != Mathf.Infinity)
    {
    if (angle > 180.0f) { angle -= 360.0f; }
    _body.angularVelocity = axis * (angle * Mathf.Deg2Rad * rotateSpeed);
    }
    }

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 2 lety +1

      Thanks!

    • @alexs7023
      @alexs7023 Pƙed 2 lety +1

      Life saver, thank you so much

    • @juandicruck
      @juandicruck Pƙed rokem +1

      Good samaritan

    • @rgi2696
      @rgi2696 Pƙed 6 měsĂ­ci

      I was looking for that for smth like 2 days!
      I love u dude!
      I mean, not fr
      But if u don't have boyfriend/girlfriend then u know...

  • @gabrifs_
    @gabrifs_ Pƙed 2 lety +4

    Bro, thank you for all these videos, I've been working with Unity for 4 years now, and recently got a Quest 2 and wanted to start developing for VR, your videos are super helpfull.
    Thanks again!

  • @armandecourtot4409
    @armandecourtot4409 Pƙed 3 lety +5

    i hope you upload that next video, i really need to know how to not make my hands just phase through objects. AWESOME VIDEOS bro keep up the work

  • @b3armonk
    @b3armonk Pƙed 3 lety +3

    Will look forward to part 2!

  • @nullname68
    @nullname68 Pƙed 3 lety +18

    I fixed the hands a little, I added a time.deltatime to body.velocity = (positionWithOffset - transform.position).normalized * followSpeed * distance * Time.deltaTime; and body.angularVelocity = angle * axis * Mathf.Deg2Rad * rotateSpeed * Time.deltaTime; . that made the hands more stable, but the hands were moving too slow so I changed Follow speed to 1800 and rotate speed to 6000. You also should move the player inside XR rig so if you walk around the hands are more stable.

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 2 lety

      Thanks!

    • @titanstudios6722
      @titanstudios6722 Pƙed 2 lety +1

      I tried doing that but it is still really slow

    • @David-gu8hv
      @David-gu8hv Pƙed rokem

      instead of:
      body.velocity = (positionWithOffset - transform.position).normalized* followSpeed * distance * Time.deltaTime
      try:
      body.velocity = (positionWithOffset - transform.position) / Time.fixedTime
      this should give the velocity needed to get to the target in one frame...

  • @lucasmartinic2039
    @lucasmartinic2039 Pƙed 3 lety +1

    Great content man, thanks!

  • @loukenart
    @loukenart Pƙed 3 lety +1

    Awesome bro! i use my own 3d hand models and it works just fine! i had one little issue in the other tutorial on how to animate the hands. i did everything but it just didn't work for some reason. but i fixed it i just watched an other tutorial and then i came back to this tutorial. im exited for the hand pose tutorial for when you grab objects! see ya soon

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 3 lety +2

      Glad you got it working! Yea some of the most recent updates have broken the hand tracking a bit. Hard to keep up with all the changes Unity makes 😅

  • @jack8750
    @jack8750 Pƙed 3 lety +4

    Looking forward to the next part, great stuff!

  • @Zippiii
    @Zippiii Pƙed 2 lety +1

    I'm in the works of learning VR game design using all of your tutorials and loving them!! Having a slit issue with floats converting the vector3 with this exact code. Would you have any idea on how to get around this issue?

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 2 lety

      Great to hear! Join the discord if you're still having issues and we can help you troubleshoot!

  • @rahulkumarray7468
    @rahulkumarray7468 Pƙed 4 měsĂ­ci

    Hi Justin, thanks for the detailed Video. I'm using oculus integration sdk and in my ovrhands, i have attached colliders and the tracking works really well. However, I am unable to manage grab and other interactions like earlier.

  • @Clamaz
    @Clamaz Pƙed 2 lety

    Hi there, great video! Everything works fine except for one small thing: when the hands collide with a box collider they take a lot of time to return the right position (the follow target one) any idea to solve this?

  • @martinstatelov3969
    @martinstatelov3969 Pƙed rokem +1

    Great tutorials :)

  • @JustinPBarnett
    @JustinPBarnett  Pƙed 3 lety +5

    What video/tutorial do you want to see next?! Reply to this comment and let me know! 👇

    • @thebutterappletutorials6553
      @thebutterappletutorials6553 Pƙed 3 lety +5

      After the hand videos, would love to see a physics body!! Or a full-body sorta rig at some point!

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 3 lety +2

      @@thebutterappletutorials6553 absolutely!! you got it!

    • @thebutterappletutorials6553
      @thebutterappletutorials6553 Pƙed 3 lety +1

      @@JustinPBarnett Yesss!!!

    • @matteomeysel
      @matteomeysel Pƙed 3 lety +2

      I know it's not really related to this video but I'd love a video about how to properly make tools (like weapons or actual tools) in VR which you can pick up and then use all the inputs from the controller that's holding it to activate something. I know that you can easily do that with the activate-function, but that's only for the trigger and I'd also want to use the A and B Button or the stick.
      Anyways great video!

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 3 lety +4

      Good idea!! I'm definitely going to do a VR gun video in the future! probably after all these hand tutorials

  • @genofelice6034
    @genofelice6034 Pƙed rokem

    I can't seem to find the additional videos for physics hands

  • @user-od7ce5lo1m
    @user-od7ce5lo1m Pƙed 2 lety

    I had a trouble when I tried to attach the hands to the controllers,. It was solved when I replace the Update() function with FixedUpdate() function. BTW, Thanks for the great videos with super high quality!!

  • @alekjatuszyk4976
    @alekjatuszyk4976 Pƙed 2 lety +2

    i have looked for tutorial like this for sooooooo long. you have saved me! thanks a lot

  • @mariusv9872
    @mariusv9872 Pƙed rokem +1

    10:44 what's the point of calculating the vector between the position of the gameobject (transform.position) and the position of the target in line 58, and then normalizing it if it's to remultiply it by its distance behind that we calculated in line 57. Because then we find the non-normalized vector if we multiply it by its distance

  • @hiddespaander749
    @hiddespaander749 Pƙed 3 lety +1

    Really need a part 2

  • @ParksInYourPocket
    @ParksInYourPocket Pƙed 3 lety +1

    Haven't watched all your videos yet. Has the second part of this come out?

  • @nohandlearchive
    @nohandlearchive Pƙed 2 lety +1

    No clue, if people are having issues with this, but the adding the absolute offset didn't quite match the correct position for me. The position only matched at a specific controller position and rotation.
    So to calculate the correct target position, I used "Vector3 targetPos = _followTarget.TransformPoint(positionOffset)" , where positionOffset is the local offset to the hands from the controller.
    TransformPoint calculates the world position from a local position.

  • @atomicalgaming
    @atomicalgaming Pƙed 2 lety +2

    Love the tutorials! I'm having one slight issue though and I'm not sure if I did something wrong. The rotation seems to be too slow and if I turn my hand relatively fast its very obvious that the rotation is slow. I have all the same variable values but the hand still seems to rotate too slow. If I turn up the rotation speed I get the vibrating that you talked about. Thank you!

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 2 lety

      Set the rigidbody.maxAngularVelocity = float.PositiveInifinity;

    • @atomicalgaming
      @atomicalgaming Pƙed 2 lety

      @@JustinPBarnett Awesome thank you!

  • @fashvr9961
    @fashvr9961 Pƙed 8 měsĂ­ci

    Hey Justin, how would you combine this with ik

  • @satirical_snake
    @satirical_snake Pƙed 2 lety +1

    Thanks for the tutorials. They help a lot! I am having an issue where the hands are not rotated properly to match the controller rotation, EVEN if I rotate them or the controllers in the editor. Can you help me with this? Thanks!

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 2 lety

      You're welcome! Are you making sure to adjust the offsets and not the actual hands?

    • @satirical_snake
      @satirical_snake Pƙed 2 lety

      Thank you! It worked

  • @olivername
    @olivername Pƙed 11 měsĂ­ci

    hey how do i stop hands going thought walls

  • @Official_KB_Studios
    @Official_KB_Studios Pƙed 2 lety

    Hey man are you ever going to make procedurally animated hand tutorial?

  • @TSalispahic
    @TSalispahic Pƙed rokem

    I have no idea if I'll get a response, but I got an issue that came from this. I followed all the tutorials about getting the hands to be rotated correctly. After adding the physics, the hands will not rotate correctly, regardless of what I do in the inspector. Any help would be appreciated, the code is almost identical to this video outside of some minor changes. Plus the code for the physics grab.

  • @tomtomh5
    @tomtomh5 Pƙed 2 lety +1

    Could this be used to interact with other humanoid models? For example if you had a human model with a rig (from blender) and an animation where said human tried to punch you, could you effectively grab its arm as if you were fighting against it to stop it punching you?

  • @joshuakelly1198
    @joshuakelly1198 Pƙed 2 lety

    Is this still nessacry relevant to your 2022 video where the hands are children of the controllers
    I'm running into a lot of issues after following that video and then onto this one
    do i need the follow and rotation if they just stay as children under the controller

  • @watermelonrolls
    @watermelonrolls Pƙed 3 lety +3

    The distance is calculated between the followObject and the transform.position. But the relative ration is calculated between the followObject and the _body. Shouldn’t these two be consistent in using the same pair?

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 3 lety +1

      Yea good catch! I fixed this in my Inverse Kinematics video

  • @sheetanshus5179
    @sheetanshus5179 Pƙed 3 lety +1

    How to get the velocity of Left and Right from controller movement ? Action mapping gives zero every time, except one but it's velocity tracking from either controller.

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 3 lety +1

      You can join the discord for some help if you need any!

  • @danielgarciagonzalez2391
    @danielgarciagonzalez2391 Pƙed 2 lety +1

    Great video. So, second part :P?

  • @tkpisamonke
    @tkpisamonke Pƙed rokem

    my hands are jittering alot can you help me with this

  • @theytpguy7658
    @theytpguy7658 Pƙed 2 lety +1

    For some reason when I click in the hierarchy to go to prefabs and unpack the XR Rig. Prefabs does not show up. It’s very strange, I don’t know what I’m doing wrong or what the issue is. These tutorials are awesome though! Keep up the good work!

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 2 lety

      You can only unpack prefabs, so if it's not blue you won't see a prefabs option

  • @RealityScapeStudios
    @RealityScapeStudios Pƙed 2 lety +1

    Did the second video come out about objects in your hands?

  • @schaki05
    @schaki05 Pƙed 3 lety +1

    At first, thanks for your tutorials. I animated the hands with your older video and tried to implement the physics from this video. The physics work for the most part but the animation is gone. How can i add the animation again?

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 3 lety +2

      Well I removed all the animation code in this video. You could just keep those in

    • @schaki05
      @schaki05 Pƙed 3 lety +1

      @@JustinPBarnett Well I did. I did not delete anything from before. But the animation is gone anyways.

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 3 lety +1

      @@schaki05 you mean the animation file is gone?

    • @schaki05
      @schaki05 Pƙed 3 lety

      @@JustinPBarnett The files are still there. The Animation ist just not happening when I pres the buttons. I just implemented everything from this video after i completed the animated hands videos and did not delete anything.

    • @schaki05
      @schaki05 Pƙed 3 lety +3

      @@JustinPBarnett found it now. The Speed was set to 0 somehow. Went through everything to find it. But thanks anyway. Now i have to solve my problem with the IK. My Camera is stuck to the ground somehow :)

  • @ollyfry3560
    @ollyfry3560 Pƙed 3 lety

    video worked great! but i do have some bugs i was hoping you could help with,
    im using a locomotion system which does continuous moving and turning, but if i try and move forward my hand gets in the way, meaning i either have to stretch my arm out so i can move forwards or just live with the fact i cant move forwards, also my hands sometimes collides with my own body from the xr rig, meaning it launches me out of the map, i also had this problem with my grab-able, which is a sphere, any help?

    • @ollyfry3560
      @ollyfry3560 Pƙed 3 lety

      also, side problem, my animations arnt running anymore, cant figure out why, seeing as the animator still has the controller and avatar set

    • @basvm6123
      @basvm6123 Pƙed 3 lety +1

      @@ollyfry3560 make sure animation speed is more than 0 and youre second problemn edit project setting physics scroll to the bottom and uncheck youre body layer or make a body layer for youre xr rig and hands and grabable layer and unmark layer collosion and it will be fixed also sorry for bad english

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 2 lety

      Join the discord if you're still having issues and we can help you troubleshoot!

  • @linomatheus7418
    @linomatheus7418 Pƙed rokem

    If you feel like your hands are rotating too slowly, remember to set the "rigidBody.MaxAngularVelocity" to a higher number. i believe default is 7.

  • @olivername
    @olivername Pƙed 10 měsĂ­ci

    hi will coilder stop them going thought objects walls without the other code

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 10 měsĂ­ci

      Yes, but the code is to keep the collider aligned on the player.

  • @_Garm_
    @_Garm_ Pƙed 3 lety +1

    for curiosity, would it still work if i just added the collisions but , keeping the normal Xr control of the hands ?

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 3 lety +2

      You'd get some weird physics effects because the hands will be teleported to where your controller is every frame instead of moving there by physics, so eventually you'd have your hands teleport inside of something when you tried to interact with it and that'd cause things to fly off sporadically

    • @_Garm_
      @_Garm_ Pƙed 3 lety +1

      @@JustinPBarnett thank you :D

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 3 lety +2

      @@_Garm_ sure thing! it's fun to try and see everything shoot off in different directions lol

    • @_Garm_
      @_Garm_ Pƙed 3 lety +1

      @@JustinPBarnett haha could be game idea just there ,the "Hot potato game" :P

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 3 lety +1

      hahaha for real

  • @HienNguyen-jk4no
    @HienNguyen-jk4no Pƙed 2 lety +1

    how did you do it can you share with me , thank you

  • @Lonely-_-Beaver
    @Lonely-_-Beaver Pƙed 2 lety +1

    I've just followed this tutorial but now my hands don't animate or pick up objects from the last tutorial :( What have I done!? Also love the tutorials thank you for helping me learn!

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 2 lety +1

      Join the discord and we can help you troubleshoot!

    • @CrewsterEX
      @CrewsterEX Pƙed 2 lety +1

      You have to change the Animation Speed on the Hand script because you change the float from speed to AnimationSpeed so it defaults to 0.

  • @thebutterappletutorials6553
    @thebutterappletutorials6553 Pƙed 3 lety +3

    Just tried this out, one issue I've run into is that when I'm rotating my left hand when it hits a certain degrees it rotates the opposite direction till it reaches where I was, any fix for that?

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 3 lety +5

      Yea there's a pretty easy fix for that! After the .ToAngleAxis() method do a check to see if the angle in degrees is greater than 180. If so, then subtract 360 from that number. This will always give you a range between -180 and 180 and should fix any weird over-rotation.

    • @thebutterappletutorials6553
      @thebutterappletutorials6553 Pƙed 3 lety +3

      @@JustinPBarnett Works like a charm thank you!

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 3 lety +3

      @@thebutterappletutorials6553 perfect!!

    • @sillyoldbear5379
      @sillyoldbear5379 Pƙed 3 lety +1

      @@JustinPBarnett Hey Justin, I'm having the same over-rotation issue. Could you please tell me how to check the angle in degrees? I'm looking at the .ToAngleAxis method in my script, but I don't see any area where it shows angle in degrees or how to calculate it. I've been searching for a solution. Thanks!

    • @Bryggyth
      @Bryggyth Pƙed 3 lety +4

      ​@@sillyoldbear5379 Had the same problem and implemented his solution. The .ToAngleAxis function has that part "out float angle" - that variable "angle" is being set by the function, and is the difference in the angles of the two objects. If the value of it is greater than 180, you want to subtract 360 from it so that it stays within -180 to 180. So the code becomes:
      q.ToAngleAxis(out float angle, out Vector3 axis);
      if(angle > 180.0f) { angle -= 360.0f; }
      _body.angularVelocity = axis * (angle * Mathf.Deg2Rad * rotateSpeed);
      It just adds one extra step between the two lines. I'm certainly not an expert, but that is what worked for me!

  • @Tecfixdirect
    @Tecfixdirect Pƙed 2 lety +1

    anyone willing to discord and help me on screenshare, i have nearly it just struggle with the animation. is there no way to just import it :(

  • @mg1632
    @mg1632 Pƙed 3 lety +1

    Why not have each hand just have a mesh collider so you don't have to do each part of the hand?

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 3 lety +1

      You can try it that way! I haven’t given it a shot so don’t know if it would work when the hand animates. Let me know what you find out! I’m curious cause that’d be much easier

  • @westparezal
    @westparezal Pƙed 2 lety +1

    why you just don`t use XR Direct Interactor for Physics?)

  • @stail2394
    @stail2394 Pƙed 2 lety +1

    Is there a way to like skip over the animation part?

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 2 lety

      You mean like comment it out?

    • @DiegoSanchez-ly4xo
      @DiegoSanchez-ly4xo Pƙed 2 lety

      @@JustinPBarnett i think he means just add colliders so you can interact, no animations, i also need something like this because my hands are just cubes i just need them to collide and pick up

  • @rattlegoat
    @rattlegoat Pƙed 3 lety +1

    anyone else having issues not being able to move after this tutorial? the hands move, but only about .01 units before stopping. any help?

    • @praxtron
      @praxtron Pƙed 2 lety +2

      I didn't have the same issue, but a similar issue, go back and look at his hand script and make sure yours is 100% exactly like his at the last portion of when he shows his final code. There's a part where he replaces a variable, I missed it on one thing and was having minor bugs as well

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 2 lety

      👆

  • @rylandwilson739
    @rylandwilson739 Pƙed 2 lety +1

    Instead of moving with the controller positions, my hands just fly away. Any solution to this?

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 2 lety

      Try teleporting them to the controllers' positions on start. You may be starting out touching another collider and that causes the flying away thing

    • @rylandwilson739
      @rylandwilson739 Pƙed 2 lety

      @@JustinPBarnett I will try that, thank you

    • @Tu-madre6969
      @Tu-madre6969 Pƙed 2 lety

      Did you manage to find a solution, I've the same problem

    • @rylandwilson739
      @rylandwilson739 Pƙed 2 lety

      @@Tu-madre6969 I never was able to solve it, no

    • @Tu-madre6969
      @Tu-madre6969 Pƙed 2 lety

      @@rylandwilson739 ok

  • @ErnestoConfused
    @ErnestoConfused Pƙed 2 lety +1

    just commenting for the algo. Thanks, m8!

  • @arfiandi3998
    @arfiandi3998 Pƙed 2 lety +1

    I have problem,my hands are shaking a little everytime

    • @JustinPBarnett
      @JustinPBarnett  Pƙed 2 lety

      Check out the video coming out today about gorilla tag movement. It should help clear some stuff up.

  • @CSHorseBobJoe
    @CSHorseBobJoe Pƙed rokem +1

    I tried making these hands, and the hands are far away from the actual controllers, and the hands float up and down slowly like 0 gravity, and sometimes they collide they start flying everywhere, but the controlling of the hand kinda works
    Edit: Fixed it.

  • @NULL3D
    @NULL3D Pƙed 2 lety +1

    Hey, there's a good chance you will not have time to read this but If you do, I would like to see a tutorial on stabbing in vr. I 100% understand If you won't be able to do one.

  • @JustinPBarnett
    @JustinPBarnett  Pƙed 3 lety +4

    Download the Project (for FREE): www.vrcreators.io/codedownloads

  • @somerandomguy2892
    @somerandomguy2892 Pƙed 3 lety +2

    the most important part of any vr game is to have a Rigidbody controller with NO Translate, No Transform, only AddForce or .Velocity.