Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jkh13 · Dec 02, 2013 at 11:48 PM · camerarotationtransformmathproject

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:

alt text

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:

  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.

alt text

unitytransform.png (9.3 kB)
f6.jpg (432.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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?)

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jkh13 · Dec 03, 2013 at 07:31 PM 0
Share

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.

avatar image Tomer-Barkan · Dec 04, 2013 at 05:27 AM 0
Share

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).

avatar image jkh13 · Dec 07, 2013 at 03:56 PM 0
Share

The code above is attached to the child, the collider is also attached to the child.

avatar image Tomer-Barkan · Dec 07, 2013 at 04:01 PM 0
Share

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.

avatar image jkh13 · Dec 07, 2013 at 04:17 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges