Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  grabbing rope and holding on to it
#1
Hi,
I am working on a VR game, I want my player hands to grab a rope with one hand and swing on it.
But I ran into some issues, essentially the player is able to grab the rope initially, but I not able to get my player to hold on to the rope when he moves (he is able to move very far away from the rope after grabbing it, but the shape of the rope looks good though).
I want the rope constrain his move once it's grabbed, I don't want the rope to strech much.
Note that I don't have a ton of experience with Unity (apologies in advance for any basic question). But I did some research, including this useful thread http://obi.virtualmethodstudio.com/forum...-2907.html.

So far I got the grabbing part partially working by:
1- Modifying the ObiContactGrabber slightly to create an attachment to the right hand game object when the player is able to grab the rope (instead of setting the grabbed particle inverse mass to 0).
I set the attachment type to dynamic. Otherwise the rope seemed to strech to the infinite when I tested (my understanding is that I need 2 way coupling).
2- adding ObiCollider and ObiRigidBody script to the hand game object.
Initially ObiRigidBody and RigidBody have kinematic (and kinematic for particles) set to true (otherwise I wasn't able to grab the rope, not sure why yet, I need to research it still)
When the particle is grabbed my script sets kinematic to false on the RigidBody and ObiRigidBody. That way the rope does not strech much when I move the player (and its hand).
The code run when grabbing the particle is successful looks like:
       
Code:
var solver = particle.solver;
        var actor = solver.particleToActor[particle.index].actor;
        var group = ScriptableObject.CreateInstance<ObiParticleGroup>();
        group.particleIndices.Add(particle.index); // index of the particle in the actor
        handAttachment = rope.AddComponent<ObiParticleAttachment>();
//player transform is the right hand transform here
        handAttachment.target = playerTransform;
        handAttachment.particleGroup = group;
        handAttachment.attachmentType = ObiParticleAttachment.AttachmentType.Dynamic;
//this was needed so the rope doesn't stretch indefinitely when player moves far away from it
// setting kinematic to false for the hand rigidBody
        ObiRigidbody orb = playerTransform.GetComponent<ObiRigidbody>();
        orb.kinematicForParticles = false;
        Rigidbody rb = playerTransform.GetComponent<Rigidbody>();
        rb.isKinematic = false;

Note that I am using continuous locomotion in VR to move the player (all based on UnityXR, XRInteraction toolkit).

So my current issue is that when the player grabs the rope then moves far from it, the shape of the rope looks good (doesn't stretch) but the player can still move very far.
But I want the player's hand to be stuck to the rope particle.
The concept seems to work with dynamic attachment in general (I was able to get a simple pendulum working).
So I suspect this is an issue related to logical conflicts between VR player locomotion and the Obi solver logics. I need the rope to 'pull back' the hand when it goes too far.
Any suggestion on how I can get this implemented?
I could probably figure out some scripting logic to force repositioning the hand (and player) close to the particle in one of the unity update() methods but not sure if I will succeed, and I was hoping for a simpler solution.

Eventually I want my player to be able to swing on the rope when he jumps (so hand and player stuck on the rope and no infinite rope stretching is definitely important). Not surehow/ if this will impact the solution.
Thanks in advance for the help.
Reply
#2
Hi!

So this is more a question about physics and rigidbodies in general than it is about Obi.

The Rigidbody component in Unity endows an object with physical properties (velocity, mass, etc)  and drives it using physics. Attaching a rope to a rigidbody using a dynamic attachment enables two-way coupling between them, that is: the rope is able to apply forces to the rigidbody, and the rigidbody is in turn able to apply forces to the rope.

So as long as your player is moved around in a physically consistent way it should all just work. By "physically consistent" I mean modifying its velocity somehow, for instance by adding forces or impulses. If you set its transform position directly, you're entirely bypassing physics simulation and as a result the player will ignore any forces applied by the rope allowing him to stretch the rope indefinitely.

I'm fairly sure that Unity's built-in XR locomotion uses no physics/rigidbodies but a CharacterController, so it will ignore physics entirely: https://docs.unity3d.com/Packages/com.un...based.html
https://www.youtube.com/watch?v=4WiMogkep1U

As stated in Unity's manual:
https://docs.unity3d.com/ScriptReference...oller.html

Quote:A CharacterController allows you to easily do movement constrained by collisions without having to deal with a rigidbody.
A CharacterController is not affected by forces and will only move when you call the Move function. It will then carry out the movement but be constrained by collisions.

If that's the case, you'll need to find a physics-based locomotion method that can be affected by forces. This is necessary not just for rope interaction, but to allow the player to be affected by any external forces other than just static collision geometry.

kind regards,
Reply
#3
(29-06-2023, 06:23 AM)josemendez Wrote: Hi!

So this is more a question about physics and rigidbodies in general than it is about Obi.

The Rigidbody component in Unity endows an object with physical properties (velocity, mass, etc)  and drives it using physics. Attaching a rope to a rigidbody using a dynamic attachment enables two-way coupling between them, that is: the rope is able to apply forces to the rigidbody, and the rigidbody is in turn able to apply forces to the rope.

So as long as your player is moved around in a physically consistent way it should all just work. By "physically consistent" I mean modifying its velocity somehow, for instance by adding forces or impulses. If you set its transform position directly, you're entirely bypassing physics simulation and as a result the player will ignore any forces applied by the rope allowing him to stretch the rope indefinitely.

I'm fairly sure that Unity's built-in XR locomotion uses no physics/rigidbodies but a CharacterController, so it will ignore physics entirely: https://docs.unity3d.com/Packages/com.un...based.html
https://www.youtube.com/watch?v=4WiMogkep1U

As stated in Unity's manual:
https://docs.unity3d.com/ScriptReference...oller.html


If that's the case, you'll need to find a physics-based locomotion method that can be affected by forces. This is necessary not just for rope interaction, but to allow the player to be affected by any external forces other than just static collision geometry.

kind regards,

Thank you so much for replying so quickly!
This makes sense.
Yesterday I made changes to the 'player' to make it be fully physics based as only the hands were using physics previously (I used https://www.youtube.com/watch?v=gk0EBIe6ZN8 as a tutorial) and it mostly works now! The rope doesn't stretch when I pull it far, and the hand is 'pulled' back by the rope, truly stays attached to the particle it grabbed.
I need to make some tweaks next (e.g. hand positioning relative to the rope after grabbing doesn't look good yet), I will try to improve things and let you know if I have more questions.
Thanks for the help!
Reply