Search Unity

How to throw objects in the correct direction?

Discussion in 'Scripting' started by Khai3, Nov 15, 2018.

  1. Khai3

    Khai3

    Joined:
    Aug 13, 2018
    Posts:
    16
    Hey guys,

    I am currently writing a code for the Vive. I have wrote a script to pick up and throw objects. However, I have trouble getting them to move in the right direction.
    I tried TransformVector and TransformDirection to get the vectors in world space but it is still not working like I want it to. This has been killing me for awhile now and I have absolutely no idea what's wrong.

    Code (CSharp):
    1.   void OnTriggerStay(Collider col2)
    2.     {
    3.         //Release Object
    4.      
    5.         if (SteamVR_Input._default.inActions.GrabPinch.GetStateUp(SteamVR_Input_Sources.LeftHand))
    6.         {
    7.             string parent = col2.transform.parent.name;
    8.             col2.attachedRigidbody.isKinematic = false;
    9.            
    10.             col2.isTrigger = false;
    11.             col2.transform.parent = null;
    12.             if (parent == "Controller (left)")
    13.  
    14.             {
    15.                 Vector3 LControlV= transform.TransformDirection(PoseData.GetVelocity(SteamVR_Input_Sources.LeftHand));
    16.                 Vector3 LControlAV = transform.TransformDirection(PoseData.GetAngularVelocity(SteamVR_Input_Sources.LeftHand));
    17.                 col2.attachedRigidbody.velocity = LControlV;
    18.                 col2.attachedRigidbody.angularVelocity = LControlAV;
    19.             }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    why don't you simply attach (parent) the object to your controller?

    -ch
     
  3. Khai3

    Khai3

    Joined:
    Aug 13, 2018
    Posts:
    16
    I didn’t include the code here but that was what I did actually. That’s why I used ‘col2.transform.parent=null’ in the code above when trying to throw the object. Right now, I am trying to add velocity once the object is thrown but the direction is wrong.
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Ah. Is it possible that PoseData.GetVelocity already returns it's value in world coordinates? If so, your additional TransformDirection will interfere.