- Home /
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 = GameCamera.Instance.camera.WorldToViewportPoint(transform.parent.position);
debugScreen = new Vector2(screenPosition.x, screenPosition.y);
// Converters for viewport -> local coordinates from camera point.
float height = 2f * GameCamera.Instance.camera.orthographicSize;
float width = height * GameCamera.Instance.camera.aspect;
// 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.
EDIT:
Here are screenshots of all the relevant components and objects during the running of the game:
The parent object that holds the rigidbody and scripts to detect collision. It;s script modifies the transform of itself only.
The working example of what I am trying to achieve. The scripts + structure of children is identical to the non-working example.
The child object of the working example holding the script above and the collider.
The parent object of the non working example.
The Collider + child of the non working example.
Answer by Tomer-Barkan · Dec 03, 2013 at 06:49 AM
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?)
I don't believe this is the problem. The red square is at position debugV3 as well. I want the position to be on the camera plane hence the z=0. The camera doesn't need to render it specifically as it is a box collider but the gizmo is showing it in the wrong position anyways. Setting z > 0 is semantically the same thing as the plane of positions would be parallel to the camera near fustrum.
If you see the parent and the child in different positions, there is most likely something in their relationship or the location of the collider relative to the transform.
Can you please share the code where you set the parent transform and the child transform? (I see in your code transform.position
but am not sure if this is the parent or child)
Please also share the inspector view of the child transform when it's fully expanded (the transform details and the collider details).
The code above is attached to the child, the collider is also attached to the child.
And the red square is the parent? Where is the code that sets its position?
And I'll need the child view in the inspector, the one that shows it's transform and the collider's details if I'm to be of any help.
I attached a bunch of screens from the inspector of all affected objects. The red square is me pausing the game, creating a 5x5 cube object and manually setting its transform to the value shown in debugV3 on the collision to viewport script.
Your answer
Follow this Question
Related Questions
How do I set a child object to not rotate if the parent WILL be rotating? 1 Answer
An alternative to RotateAround for collisions 0 Answers
Issue with transform rotations and local rotations 1 Answer
Clamping cameras with different rotation 0 Answers
How to limit the angle of a camera with transform.RotateAround? 1 Answer