Getting Position and Orientation (Rotation) of Light object with respect to camera

Hello,

I’m new to Unity and C#, I wanted to ask for an environment where I have 1 light source, and multiple cameras (4) at different locations, how can I get the position & rotation (i.e. orientation) of the light source with respect to the different cameras.

I tried with
InverseTransformDirection &
InverseTransformPoint

I’ve written a script like this:

foreach(GameObject go in GameObject.FindGameObjectsWithTag("MainCamers"))
        {
            // Get the camera:
            Camera cam = go.GetComponent<Camera>();
            TransformMatrix = go.transform;
            // calculate transformation matrix... get light pos relative to camera
            // get light component
            _light = GetComponent<Light>();
            // get light position, orientation & intensity:
            _light_pos = _light.transform.position;
            RelativePos = TransformMatrix.InverseTransformPoint(_light_pos);
            _light_orient = _light.transform.rotation;
            RelativeOrient = TransformMatrix.InverseTransformDirection();
}

However, I’m still struggling to understand whether I’m understanding this problem / formulation correctly or not.
Any help would be highly appreciated.

What is it you are trying to do? Rotate the light to face the active camera? At the moment you setting its position and rotation multiple times.

Aaah I didn’t realise I was doing that :eyes:, I am doing a robotics project, so I just want the gameObject in my case the light’s position relative to the camera. So on the Unity environment I set the light position, though that’s in the 3D world coordinates, I want it’s position relative to the camera such that, x and y are local pixel coordinates and z is the depth (so still world).
And I want to extract this for each camera

So tldr; I want the light’s position & orientation to stay as it is, I just want to convert it’s position & orientation relative to the cameras.

Would this be right?
If I made the light a child of each of the camera object and then extracted it’s transform.localPosition?

foreach(GameObject go in GameObject.FindGameObjectsWithTag("MainCamers"))
        {
            // Get the camera:
            Camera cam = go.GetComponent<Camera>();
            // get light component
            _light = GetComponent<Light>();
            // make light the child of camera object; 
            _light.transform.parent = cam.transform;
            //_light is now the child of cam
            // get relative light position, orientation & intensity:
            _lightPos = _light.transform.localPosition;
            _lightOrient= _light.transform.localRotation;
}

You are still setting the variable multiple times. What do you mean by covert? What is it you want to do with the data?

erm… well I guess, convert is a poor choice of word, I just want the light position with respect to the cameras not as world position.
with the data, I just want to store it as a json file. I later want to plot / track the changes as the camera moves around the environment on a fixed trajectory.

If you want the difference between two vectors just subtract them. If you want multiple positions, store each vector in a list.

Yes, that’s what I’m aiming to do :slight_smile: thanks.
Though I just wanted to confirm that will the following;

_lightPos = _light.transform.localPosition;
_lightOrient= _light.transform.localRotation;

Actually give me what I want, the relative position of light to the camera?