Transform.position of child object not working as intended.

I am trying to transform a child object of a gameobject into the near fustrum screen plane of the viewing Orthogonal camera. The child object is a box collider and the parent contains a rigidbody with isKinematic = true (no physics). This generally works but I noticed it bugging out every now and then. Upon further debugging I noticed some really strange behavior.

The code is below:

private static Quaternion rot = Quaternion.Euler(new Vector3(60, 0, 0));

// Debug
public Vector2 debugScreen;
public Vector3 debugV3;

void Update () {
    // Align rotation to face the screen.
    transform.rotation = rot;

    // Get parents position on screen plane (viewport co-ords).
    Vector3 screenPosition =;
    debugScreen = new Vector2(screenPosition.x, screenPosition.y);

    // Converters for viewport -> local coordinates from camera point.
    float height = 2f *;
    float width = height *;

    // Transform point into correct position on camera near fustrum plane.
    debugV3 = GameCamera.Instance.transform.TransformPoint((screenPosition.x - 0.5f) * width, (screenPosition.y - 0.5f) * height, 0);
    // Set this objects location to this point.
    transform.position = debugV3;

It should be pretty self-explanatory. I am projecting the point onto the viewport, converting to local coordinates, then getting the resultant world coordinate and setting the child objects world position to the point.

So the odd part is that debugV3 and debugScreen give me the correction positions whereas when I move the child object to the same position it is sometimes incorrect. The picture below illustrates this:


The green square is the collider from the child container in the wrong position.
The axis is the parent transform (in the correct position).
The red square is a separate game object with position set to debugV3 and rotation to rot, this is in the correct position.
What is not shown is transform of the object from the last line of the code. This is centered where the box collider is.

I am setting the value to debugV3 in the last line of the code but it is still not in the correct position for some reason. I have no idea what is going on, am I missing something here?

When the vehicle moves the box is supposed to keep following transform.parent as shown in the code. Most of the time it is correct and is in the same position as the red square but sometimes it oscillates off the position for a few secs then returns to the correct position gradually.


Here are screenshots of all the relevant components and objects during the running of the game:

  1. The parent object that holds the rigidbody and scripts to detect collision. It;s script modifies the transform of itself only.

  2. The working example of what I am trying to achieve. The scripts + structure of children is identical to the non-working example.

  3. The child object of the working example holding the script above and the collider.

  4. The parent object of the non working example.

  5. The Collider + child of the non working example.

Your problem is here:

    debugV3 = GameCamera.Instance.transform.TransformPoint((screenPosition.x - 0.5f) * width, (screenPosition.y - 0.5f) * height, 0);

You’re setting debugV3.z to be the exact same z of the camera (you passed 0 to TransformPoint), so the camera is unable to display it. You’d want to set it to be a few units in front of the camera (or maybe use the parent’s own z coords?)