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 czadam · Nov 21, 2012 at 10:47 AM · rotationpositiondirection

3d model orientation

I have a camera, which can rotate and move in the world freely, in every direction. I have a 3D object, and when I push a button, I want that object to "fly" (animate) in front of my camera, in good orientation, no matter in what position my camera is. See the picture: I want this instrument to animate to the camera, and I want to see it always in that position relative to the camera. (Of course, once it is in the good position I can parent it to the camera.) I also want to scale the model, that it can fit in the viewport of the camera. How can I do this?

alt text

I edited my code as Berenger suggested, but I am doing something wrong, please someone review my code, maybe you can spot the mistakes.

Thx for the help, I am trying to accomplish, waht you described, but it's not seems to work yet. Please look at my code, maybe you can spot the mistakes. I was also using this pic:

alt text

 //half of the collider height
 height=gameObject.collider.bounds.size.y/2;
 
 //camera horizontal field of view        
 h_fov= Camera.main.fieldOfView;
 
 //screen ratio            
 rat= (float)Screen.height/ (float)Screen.width;
 
 //according to the linked picture: tan(vertical FOV/2)            
 tan_v_fov_per_two=rat*Mathf.Tan((h_fov/2)*(Mathf.PI/180));
             
 //distance from camera
 distance = height /  tan_v_fov_per_two;           
             
 //animate in front of the camera (my cam position: x=0,y=200, z=0)
 iTween.MoveTo(gameObject, new Vector3(Camera.main.transform.position.x,Camera.main.transform.position.y-distance, Camera.main.transform.position.z),2);


hangszer.png (24.8 kB)
6a0120a85dcdae970b0120a86d9495970b.png (9.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
Best Answer

Answer by Berenger · Nov 21, 2012 at 11:27 AM

You can use transform.LookAt to make sure the orientation is correct, with an offset depending on your mesh orientation.

For the position, Vector3.SmoothDamp or Vector3.Lerp( ..., Time.deltaTime * speed ) should do the trick.

Finally, to make sure the object fits in the viewport, you can use it's bounding box (from the renderer or the collider), whilch gives you the opposite side of the triangle made by the camera, the object's extremity (height) and the object's center. You know the camera's angle and the opposite side, so a little trigo :

 // H : hypotenuse (cam -> extremity). not used here
 // h : half  of the object's height (extremity -> center)
 // d : distance of the object from the cam (that's what you want)
 // alpha (half the cam's fov)
 
 Tan(alpha) = h / d // SOHCAHTOA, never forget !
 Tan(alpha) / h = 1 / d
 d = h / Tan(alpha)

Tadaaa

I did not tested it !

Comment
Add comment · Show 2 · 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 czadam · Nov 21, 2012 at 07:41 PM 0
Share

hi! I edited my question according to your suggestion, can you look at it, what am I doing wrong?

avatar image Berenger · Nov 22, 2012 at 12:35 PM 0
Share

You should try height / 2.

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

11 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

Related Questions

How to get offset postition after rotation ? (Spent hours figuring it out) 3 Answers

How can I get the rotated position without using Transform in 2D? 1 Answer

Lerp in direction to certain distance 3 Answers

Unfreezing rotation causes the position and rotation go crazy 0 Answers

Spwan and shoot bullet in direction of shooter? 2 Answers


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