Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

TransformDirection

Discussion in 'Scripting' started by shaundavies, Jun 5, 2017.

  1. shaundavies

    shaundavies

    Joined:
    Jan 31, 2017
    Posts:
    44
    Hi,

    I am having trouble understanding the scripting transformdirection variables. Below is an example script i made and I am getting the object to manipulate the proper direction however the object is all jittery on that axis.

    Any thoughts on my code or how to manipulate local space to world space with individual components of a vector would be greatly appreciated.



    Code (CSharp):
    1. //JabController
    2. float JC_ObjX;
    3. float JC_Objy;
    4. float JC_Objz;
    5.  
    6. //Clamp
    7. float JC_minObjX;
    8. float JC_maxObjX;
    9.  
    10. bool Toggle = false;
    11.  
    12. void NeedleGrab()
    13. {
    14.       if (GameObject.Find("Needle_JabController"))
    15.       {
    16.             JC_ObjX = transform.position.x;
    17.             JC_Objy = transform.position.y;
    18.             JC_Objz = transform.position.z;
    19.             JC_minObjX = JC_ObjX - .2f;
    20.             JC_maxObjX = JC_ObjX;
    21.       }
    22.  
    23. }
    24.  
    25. // update is called once per frame
    26. void Update()
    27. {
    28.       if (OVRInput.GetDown(OVRInput.Button.SecondaryHandTrigger))
    29.       {
    30.             NeedleGrab();
    31.             Toggle = true;
    32.       }
    33.       if (Input.GetKeyDown(KeyCode.I))
    34.       {
    35.             NeedleGrab();
    36.             Toggle = true;
    37.       }
    38.  
    39.  
    40.       if (Toggle == true)
    41.       {
    42.           float x = transform.TransformDirection(transform.position).x;
    43.  
    44.  
    45.           float clamp = Mathf.Clamp(x, JC_minObjX, JC_maxObjX);
    46.           Vector3 noclamp = new Vector3(clamp, JC_Objy, JC_Objz);
    47.           this.transform.position = this.transform.transform.TransformDirection(noclamp);
    48.           this.transform.rotation = GameObject.Find("Needle").transform.rotation;
    49.        }
    50.  
    51.        if (OVRInput.GetUp(OVRInput.Button.SecondaryHandTrigger))
    52.        {
    53.              Toggle = false;
    54.              this.transform.position = new Vector3(this.transform.localPosition.x, JC_Objy, JC_Objz);
    55.        }
    56.  
    57. }
    58.  
     
    Last edited: Jun 5, 2017
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  3. shaundavies

    shaundavies

    Joined:
    Jan 31, 2017
    Posts:
    44
    Thanks Daniel I have been wondering how to do that lol.

    It looks like I have found out the problem which is to get the inverse.transformdirection and then convert that number back to transformdirection. Something like this -

    Code (CSharp):
    1. float newO = Mathf.Clamp(transform.InverseTransformDirection(transform.position).x, -5, 5);
    2.         float newo1 = Mathf.Clamp(transform.position.y, -5, 5);
    3.         Vector3 newone = new Vector3(newO, 0, 0);
    4.         this.transform.position = this.transform.TransformDirection(newone);
    If this works ill post the revised code
     
  4. shaundavies

    shaundavies

    Joined:
    Jan 31, 2017
    Posts:
    44
    In case anyone finds this of use I had to use transform vectors to get an object to move on its local axis. You must first grab the inversetransformvector values from your object, manipulate those numbers, then plug the transformvector values back into your object. To solve the jitter problem I had to apply the euler angles of the parent object and apply it to the child i was manipulating.

    Code (CSharp):
    1. float JC_ObjX;
    2.     float JC_Objy;
    3.     float JC_Objz;
    4.  
    5.     float JC_Rotx;
    6.     float JC_Roty;
    7.     float JC_Rotz;
    8.     //Clamp
    9.     float JC_minObjX;
    10.     float JC_maxObjX;
    11.     float JC_minObjY;
    12.     float JC_maxObjY;
    13.  
    14.  
    15.     bool Toggle = false;
    16.  
    17.     void NeedleGrab()
    18.     {
    19.         if (GameObject.Find("Needle_JabController"))
    20.         {
    21.             JC_ObjX = transform.InverseTransformVector(transform.position).x;
    22.             JC_Objy = transform.InverseTransformVector(transform.position).y;
    23.             JC_Objz = transform.InverseTransformVector(transform.position).z;
    24.             JC_minObjX = JC_ObjX - .005f;
    25.             JC_maxObjX = JC_ObjX;
    26.          
    27.         }
    28.      
    29.     }
    30.  
    31.     // update is called once per frame
    32.     void Update()
    33.     {
    34.         this.transform.eulerAngles = GameObject.Find("Needle").transform.eulerAngles;
    35.         if (OVRInput.GetDown(OVRInput.Button.SecondaryHandTrigger))
    36.         {
    37.             NeedleGrab();
    38.             Toggle = true;
    39.         }
    40.         if (Input.GetKeyDown(KeyCode.I))
    41.         {
    42.             NeedleGrab();
    43.             Toggle = true;
    44.         }
    45.  
    46.  
    47.         if (Toggle == true)
    48.         {
    49.             float clamp = Mathf.Clamp(transform.InverseTransformVector(transform.position).x, JC_minObjX, JC_maxObjX);
    50.           JC_maxObjY);
    51.             Vector3 noclamp = new Vector3(clamp, JC_Objy, JC_Objz);
    52.        
    53.             this.transform.position = this.transform.transform.TransformVector(noclamp);
    54.          
    55.  
    56.             GameObject.Find("Needle").GetComponent<Collider>().enabled = false;
    57. }
    58. }
    59. }
    60.            
    61.  
    62.  
    63.         }
     
  5. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    what was this supposed to do in the context of your game? inverseTransfromDirection and then applying that to TransformDirection sounds almost like (x*y)/y and does nothing, or at the very least it sounds like there are necessary steps.
     
  6. shaundavies

    shaundavies

    Joined:
    Jan 31, 2017
    Posts:
    44
    Hi Laperen. I am having an object that is made up of multiple parts and have those parts as children to a parent so that the pieces moves together but when desired can be independently moved in the constructs I want. This way I can rotate an object any direction and still have its parts constrained to the same values. Sorry to be abstract. Its like having a door u want to grab and move any direction and it has multiple locks you want to then manipulate.
     
  7. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Am I right to say you are trying to manipulate a tethered object in VR?
     
  8. shaundavies

    shaundavies

    Joined:
    Jan 31, 2017
    Posts:
    44
    yes, but this works with the drag rigidbody script given by unity