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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Get relative Location from other component?

Discussion in 'Scripting' started by Michael_R, Nov 27, 2018.

  1. Michael_R

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    29
    Hello,

    does anyone know how i can get the relative location from my camera and use it in the coordinate system of my hand?
     
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Code (CSharp):
    1. public class ExampleHand : MonoBehaviour
    2.     {
    3.         public Camera playersCamera;
    4.  
    5.         public Vector3 GetRelativeLocation()
    6.         {
    7.             return transform.InverseTransformPoint(playersCamera.transform.position);
    8.         }
    9.     }
    That will get you the position of the camera in the hand's local space.

    Look at these for reference:

    https://docs.unity3d.com/ScriptReference/Transform.TransformPoint.html
    https://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html
     
  3. Michael_R

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    29
    Can i convert this world position to a local position of my hand component?
    Or is there a easier way to get the relative 0 aka center of the camera position?
     
  4. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Yes, that's exactly what my example script does.

    playersCamera.transform.position - That is the world position camera.

    transform.InverseTransformPoint - This method takes a world position, and turns it into a local position of whatever transform you call it from. In this case we're calling it from the ExampleHand's transform. Meaning that the Vector3 returning from that method will be the hand's local position equivalent of that world position.

    Hope that helps.
     
  5. Michael_R

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    29
    do i dont need to calculate the world position of the camera
     
  6. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Not at all. Find the GameObject of the camera, and it will have a 'transform' component built in. It will have a 'position' variable. That is always the position that object is in at the time of calling it. So calling camera.transform.position, is the calculated world position of the camera.
     
  7. Michael_R

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    29
    You know i want to make ads with that, is there a 'easier' way doing that? Do you think it will work?
     
    Last edited: Nov 27, 2018
  8. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    What are you trying to do?
     
  9. Michael_R

    Michael_R

    Joined:
    Sep 6, 2018
    Posts:
    29
    Sry for my bad english. I want to make a little shooter. For that i need a Aim down Sights for my weapons.
    In this method I need to adjust the ads position in the editor that my gun sight is centered in the screen(very annoying)


    Code (CSharp):
    1.     private void AimDownSights()
    2.     {
    3.         if (Input.GetButton("Fire2") /*&& !isReloding*/)
    4.         {
    5.  
    6.             transform.localPosition = Vector3.Lerp(transform.localPosition, aimPosition, Time.deltaTime * adsspeed);
    7.         }
    8.  
    9.         else
    10.         {
    11.             transform.localPosition = Vector3.Lerp(transform.localPosition, originalPosition, Time.deltaTime * adsspeed);
    12.         }
    13.  
    14.     }

    I just want to get the relative position 0 of the camera for my ads?
    Do you understand this?