Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  3. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

convert relative/world position

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

  1. Michael_R

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    20
    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. SparrowsNest

    SparrowsNest

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

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    20
    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. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    681
    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:
    20
    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. SparrowsNest

    SparrowsNest

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

    Michael_R

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