Search Unity

I NEED HELP ! Vr Sword

Discussion in 'VR' started by AltekGames, Jun 21, 2019.

  1. AltekGames

    AltekGames

    Joined:
    Sep 11, 2017
    Posts:
    1
    Hi,

    I'm fairly new to Unity and C# in general. But I've got a VR scene set up where I have a sword and sheath. I'm trying to make it so that my Sheath is holstered to my left side. And when I go to reach for the sword, I can pick up the sword and drag it out of the sheath - I also want to be able to put it back into the sheath. The problem is I don't want to make this an Animation, instead I want the player to be able to both, draw the sword from the sheath and place it back in.

    Any ideas how I can do this???

    I would really appreciate it, been stuck on this for days!! Thanks
     
  2. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    725
    Well the simpliest way is going to be using a collider. If you want to use physics, this is not going to be an easy task.

    If you want to use physics the sword and the sheath have to have rigidbodies on them. You need to make an oversides gap between the sword and the hole it goes into, and oversized hole or whatever you call it.Make sure both the sword and the sheath have Mesh Colliders on them. Mesh Colliders are better than any other because even though they take up more memory they put a collider around every vertice of your model so the sword SHOULD fit inside the sheath then with physics.

    When the sword is in the sheath it's then going to wobble, so you want to have a collider that detects that it's in the sheath in the correct way and then it should 'lock' into it by going into a state that is 'sheathed'.

    Your first step is to think in states. Your first state is unsheathed, your next state is sheathed, however there could be more states:

    Code (CSharp):
    1. public enum State { Idle, Held, Sheathed }
    2. public State state;
    3.  
    4. void Update() {
    5.   switch (state) {
    6.     case State.Idle:
    7.        //physics make it fall to the ground.
    8.     break;
    9.     case State.Held:
    10.       //physics are turned off ( you can disable your rigidbody here )
    11.       //this is where you if conditions go to check for the swords tip hitting the bottom
    12.       // of the sheath AND the controllers trigger being released.
    13.     break;
    14.     case State.Sheathed:
    15.        //physics are turned off, the sword 'locks' into the sheath here so have it take
    16.        // on the same transform or become a child of the sheath or whatever.
    17.        //  this is also where your if conditions go to check if the player has just grabbed onto
    18.         // the swords handle.
    19.     break;
    20.   }
    21. }
    That's where I would start to be honest with your sword script.
     
    Last edited: Jun 23, 2019