Search Unity

convert relative/world position

Discussion in 'Physics' started by Michael_R, Dec 3, 2018.

  1. Michael_R

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    29
    I thought about a script to get the relative fpscamera position into my hand coordinate system. The Idea is to move the hand right into the center of my fpscamera (my Aim Down Sights Idea):

    script (lerp included):

    Code (CSharp):
    Code (CSharp):
    1.             GameObject handAK47 = GameObject.Find("FPSController/FirstPersonCharacter/WeaponHolder/M4 Garand Holder/FPS-AK47/Game_engine/Bone/AK-47");
    2.             GameObject fpscamera = GameObject.Find("FPSController/FirstPersonCharacter");
    3.  
    4.  
    5.             transform.localPosition = Vector3.Lerp(handAK47.transform.localPosition, handAK47.transform.InverseTransformPoint(fpscamera.transform.position), Time.deltaTime * adsspeed);
    ... maybe someone of you guys know what I am doing wrong ;)
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Just use .position on both.(i think)
     
  3. Michael_R

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    29
    Do you mean like this:

    Code (CSharp):
    1. transform.position = Vector3.Lerp(handAK47.transform.position, handAK47.transform.InverseTransformPoint(fpscamera.transform.position), Time.deltaTime * adsspeed);
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    The object this script in on is the AK i assume?

    Code (CSharp):
    1. transform.position = Vector3.Lerp(handAK47.transform.position, fpscamera.transform.position, Time.deltaTime * adsspeed);
    2.  
    btw, just a side note, that's not the best way to use lerp.
     
  5. Michael_R

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    29
    Still doesn't work:mad:. My hierarchy looks like this. Could you pls check if my GameObject.Find commands are wrong?

    Code (CSharp):
    1. GameObject handAK47 = GameObject.Find("FPSController/FirstPersonCharacter/WeaponHolder/M4 Garand Holder/FPS-AK47/Game_engine/Bone/AK-47");
    2.             GameObject fpscamera = GameObject.Find("FPSController/FirstPersonCharacter");

    a.PNG
     
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    What object is the script on, the FPS Controller or the AK?
     
  7. Michael_R

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    29
    The FPS Controller is where i get the camera and the ak is the weapon.