Search Unity

relative, world position question

Discussion in 'Scripting' started by Michael_R, Dec 2, 2018.

  1. Michael_R

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    29
    I thought about a script like this one to get the relative camera position of my hand coordinate system, so my hand moves to my camera (Aim Down Sights Idea):

    script (lerp included):

    Code (CSharp):
    1. transform.localPosition = Vector3.Lerp(transform.localPosition, transform.InverseTransformPoint(Camera.current.transform.position), Time.deltaTime * adsspeed);
    ... unfortunately the script doesnt work yet.
    Maybe the script doesnt know which coordinate system is meant.

    ps. sorry for my bad england
     
    Last edited: Dec 3, 2018
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    transform.localPosition uses local position, in respect to object's parent, not the world.
    But then you use inverse, with Camera absolute transform reference.
    So there may be some confusion.

    PS. please use script tag for codes.
    Instruction Using code tags properly
     
  3. Michael_R

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    29
    I just changed it as code. Which things in my script would change to make it work?
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    I am not sure, if I understand what you try achieve exactly.
    Try
    Code (CSharp):
    1.  
    2. Camera cam = Camera.current ;
    3. Vector3 positionRelativeToCamera = cam.InverseTransformPoint(transform.position);
    4. transform.position = Vector3.Lerp ( transform.position, cam.position + positionRelativeToCamera, Time.deltaTime * adsspeed);
    Haven't tested, so not sure 100% if that is correct.
    Did you look into API manual? I assume so, you did.
    https://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html
     
  5. Michael_R

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    29
    I just want to move my hand to the position of my camera.


    a.PNG
    How can I get the blue marked "AK-47" in visual studio to get the relative position of it?
     
    Last edited: Dec 3, 2018
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    If you just want move object to camera position then
    Code (CSharp):
    1. transform.position = Vector3.Lerp ( transform.position, Camera.current.position, Time.deltaTime * adsspeed)
    That is it.
    I don't know anything about Unity AK-47. I rather don't use Unity assets.
     
  7. Michael_R

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    29
    I just uploaded the picture in my last comment ;)
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    Either attach script with MonoBehavior to the AK
    Or, assign Tag to the weapon, and from other script, look for objects by tag.
    Or by name, use
    Code (CSharp):
    1. GameObject myGun = GameObject.Find ("Game_Engine\BonezAK-47") ;
    for example. Something like that.
    Just make sure, you call these at start, or initialization, or at spawning objects only.