Search Unity

Finding Rotation Of Object In Another Objects Space

Discussion in 'Scripting' started by Nigey, Mar 15, 2019.

  1. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Hi Guys,

    Struggling with something here. I have a VR hand. It interacts with a valve that I want to turn. I want the functionality to be, when you spin your hand along the axis that the valve can spin, the valve turns. I've got it 'kind of working', but it's supposed to only turn up to a certain amount in either direction, and the values are jumping all over the place in the debug.log. Can anyone see a glaring issue here?

    Code (CSharp):
    1.  
    2.         // minTurn = 0f, maxTurn = 720f, currentTurn = 0f, axis = new Vector3(0f, 0f, 1f);
    3.         protected virtual void FixedUpdate()
    4.         {
    5.             if (IsAttached == true)
    6.             {
    7.                 if (!setupInteraction)
    8.                 {
    9.                     previousAngle = AttachedHand.Transform.position;
    10.                     setupInteraction = true;
    11.                 }
    12.  
    13.                 var deltaAngle = AttachedHand.Transform.eulerAngles - previousAngle;
    14.                 var dot = Vector3.Dot(deltaAngle.normalized, axis.normalized);
    15.  
    16.                 if(dot != 0f)
    17.                 {
    18.                     var force = dot * deltaAngle.magnitude;
    19.  
    20.                     if (force + currentTurn > minTurn && force + currentTurn < maxTurn)
    21.                     {
    22.                         currentTurn += force;
    23.                         transform.Rotate(axis.normalized * force);
    24.                     }
    25.                 }
    26.  
    27.                 previousAngle = AttachedHand.Transform.eulerAngles;
    28.             }
    29.         }
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Do you mean to turn your hand in a circular motion like you are turning a valve wheel?

    If so, just look at the steamvr circular drive in their asset in the store, iirc it does everything you need.
     
    Nigey likes this.
  3. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Thanks for the tip. I used something similar.
     
    chrisrcisme likes this.