How to Make Physics-Based Hands in Unity XR

Поділитися
Вставка
  • Опубліковано 27 жов 2024

КОМЕНТАРІ • 135

  • @Piepop101
    @Piepop101 2 роки тому +26

    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  2 роки тому +1

      Thanks!

    • @alexs7023
      @alexs7023 2 роки тому +1

      Life saver, thank you so much

    • @juandicruck
      @juandicruck 2 роки тому +1

      Good samaritan

    • @rgi2696
      @rgi2696 10 місяців тому

      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...

  • @stargazerbr_yt
    @stargazerbr_yt 2 роки тому +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 3 роки тому +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

  • @nullname68
    @nullname68 3 роки тому +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  2 роки тому

      Thanks!

    • @titanstudios6722
      @titanstudios6722 2 роки тому +1

      I tried doing that but it is still really slow

    • @David-gu8hv
      @David-gu8hv 2 роки тому

      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...

  • @JustinPBarnett
    @JustinPBarnett  3 роки тому +5

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

    • @thebutterappletutorials6553
      @thebutterappletutorials6553 3 роки тому +5

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

    • @JustinPBarnett
      @JustinPBarnett  3 роки тому +2

      @@thebutterappletutorials6553 absolutely!! you got it!

    • @thebutterappletutorials6553
      @thebutterappletutorials6553 3 роки тому +1

      @@JustinPBarnett Yesss!!!

    • @matteomeysel
      @matteomeysel 3 роки тому +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  3 роки тому +4

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

  • @b3armonk
    @b3armonk 3 роки тому +3

    Will look forward to part 2!

  • @JustinPBarnett
    @JustinPBarnett  3 роки тому +4

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

  • @nohandlearchive
    @nohandlearchive 2 роки тому +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.

  • @hiddespaander749
    @hiddespaander749 3 роки тому +1

    Really need a part 2

  • @alekjatuszyk4976
    @alekjatuszyk4976 3 роки тому +2

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

  • @mariusv9872
    @mariusv9872 2 роки тому +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

  • @lucasmartinic2039
    @lucasmartinic2039 3 роки тому +1

    Great content man, thanks!

  • @martinstatelov3969
    @martinstatelov3969 2 роки тому +1

    Great tutorials :)

  • @loukenart
    @loukenart 3 роки тому +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  3 роки тому +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 😅

  • @genofelice6034
    @genofelice6034 Рік тому

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

  • @jack8750
    @jack8750 3 роки тому +4

    Looking forward to the next part, great stuff!

  • @ParksInYourPocket
    @ParksInYourPocket 3 роки тому +1

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

  • @趙胤筌
    @趙胤筌 2 роки тому

    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!!

  • @rahulkumarray7468
    @rahulkumarray7468 8 місяців тому

    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.

  • @Zippiii
    @Zippiii 3 роки тому +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  2 роки тому

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

  • @danielgarciagonzalez2391
    @danielgarciagonzalez2391 3 роки тому +1

    Great video. So, second part :P?

  • @TSalispahic
    @TSalispahic Рік тому

    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.

  • @atomicalgaming
    @atomicalgaming 2 роки тому +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  2 роки тому

      Set the rigidbody.maxAngularVelocity = float.PositiveInifinity;

    • @atomicalgaming
      @atomicalgaming 2 роки тому

      @@JustinPBarnett Awesome thank you!

  • @watermelonrolls
    @watermelonrolls 3 роки тому +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  3 роки тому +1

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

  • @Official_KB_Studios
    @Official_KB_Studios 2 роки тому

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

  • @theytpguy7658
    @theytpguy7658 2 роки тому +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  2 роки тому

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

  • @RealityScapeStudios
    @RealityScapeStudios 2 роки тому +1

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

  • @linomatheus7418
    @linomatheus7418 2 роки тому

    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.

  • @fashvr9961
    @fashvr9961 Рік тому

    Hey Justin, how would you combine this with ik

  • @NULL3D
    @NULL3D 2 роки тому +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.

  • @satirical_snake
    @satirical_snake 2 роки тому +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  2 роки тому

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

    • @satirical_snake
      @satirical_snake 2 роки тому

      Thank you! It worked

  • @Clamaz
    @Clamaz 2 роки тому

    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?

  • @tkpmonke
    @tkpmonke 2 роки тому

    my hands are jittering alot can you help me with this

  • @tomtomh5
    @tomtomh5 3 роки тому +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?

  • @thebutterappletutorials6553
    @thebutterappletutorials6553 3 роки тому +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  3 роки тому +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 3 роки тому +3

      @@JustinPBarnett Works like a charm thank you!

    • @JustinPBarnett
      @JustinPBarnett  3 роки тому +3

      @@thebutterappletutorials6553 perfect!!

    • @sillyoldbear5379
      @sillyoldbear5379 3 роки тому +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 3 роки тому +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!

  • @CSHorseBobJoe
    @CSHorseBobJoe 2 роки тому +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.

  • @Lonely-_-Beaver
    @Lonely-_-Beaver 3 роки тому +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  2 роки тому +1

      Join the discord and we can help you troubleshoot!

    • @CrewsterEX
      @CrewsterEX 2 роки тому +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.

  • @westparezal
    @westparezal 3 роки тому +1

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

  • @sheetanshus5179
    @sheetanshus5179 3 роки тому +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  3 роки тому +1

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

  • @ErnestoConfused
    @ErnestoConfused 3 роки тому +1

    just commenting for the algo. Thanks, m8!

  • @arfiandi3998
    @arfiandi3998 2 роки тому +1

    I have problem,my hands are shaking a little everytime

    • @JustinPBarnett
      @JustinPBarnett  2 роки тому

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

  • @olivername
    @olivername Рік тому

    hey how do i stop hands going thought walls

  • @joshuakelly1198
    @joshuakelly1198 2 роки тому

    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

  • @HienNguyen-jk4no
    @HienNguyen-jk4no 3 роки тому +1

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

  • @mg1632
    @mg1632 3 роки тому +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  3 роки тому +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

  • @olivername
    @olivername Рік тому

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

    • @JustinPBarnett
      @JustinPBarnett  Рік тому

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

  • @schaki05
    @schaki05 3 роки тому +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  3 роки тому +2

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

    • @schaki05
      @schaki05 3 роки тому +1

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

    • @JustinPBarnett
      @JustinPBarnett  3 роки тому +1

      @@schaki05 you mean the animation file is gone?

    • @schaki05
      @schaki05 3 роки тому

      @@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 3 роки тому +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 :)

  • @rylandwilson739
    @rylandwilson739 2 роки тому +1

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

    • @JustinPBarnett
      @JustinPBarnett  2 роки тому

      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 2 роки тому

      @@JustinPBarnett I will try that, thank you

    • @Tu-madre6969
      @Tu-madre6969 2 роки тому

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

    • @rylandwilson739
      @rylandwilson739 2 роки тому

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

    • @Tu-madre6969
      @Tu-madre6969 2 роки тому

      @@rylandwilson739 ok

  • @stail2394
    @stail2394 3 роки тому +1

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

    • @JustinPBarnett
      @JustinPBarnett  3 роки тому

      You mean like comment it out?

    • @DiegoSanchez-ly4xo
      @DiegoSanchez-ly4xo 2 роки тому

      @@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 3 роки тому +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 3 роки тому +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  2 роки тому

      👆

  • @_Garm_
    @_Garm_ 3 роки тому +1

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

    • @JustinPBarnett
      @JustinPBarnett  3 роки тому +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_ 3 роки тому +1

      @@JustinPBarnett thank you :D

    • @JustinPBarnett
      @JustinPBarnett  3 роки тому +2

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

    • @_Garm_
      @_Garm_ 3 роки тому +1

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

    • @JustinPBarnett
      @JustinPBarnett  3 роки тому +1

      hahaha for real

  • @ATiredArchivist
    @ATiredArchivist 3 роки тому

    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?

    • @ATiredArchivist
      @ATiredArchivist 3 роки тому

      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 3 роки тому +1

      @@ATiredArchivist 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  2 роки тому

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

  • @Tecfixdirect
    @Tecfixdirect 3 роки тому +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 :(

  • @somerandomguy2892
    @somerandomguy2892 3 роки тому +2

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