Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 KEric · Nov 17, 2015 at 01:48 PM · unity 5rotation3dquaternionmath

Smooth rotation to child object

I have car model which is build from different parts (separate 3d meshes such as mirrors, bumpers, wheels etc) all contained in Unity GameObject's hierarchy. The car itself stands on a cylindrical platform. Based on a part of user choice I would like it to have positioned/rotated towards the camera. For example if a mirror has been selected I would like the platform to rotate so that the mirror would become aligned with camera view. I'm attaching the code I've been working on, which of course has some errors. Help highly appreciated.

 public class PlatformBase : MonoBehaviour {
 
     private Transform selection;  // selected part
     private Quaternion q;
 
     void Update ()
     {    
         transform.rotation = Quaternion.Lerp(transform.rotation, q, Time.deltaTime);    
     }
 
     public Transform Selection
     {
         set
         {
                 selection = value;
                 angle = Vector3.Angle(
                     -Camera.main.transform.forward, 
                     new Vector3(selection.forward.x, 0.0f, selection.forward.z).normalized);
                 q = Quaternion.AngleAxis(angle, Vector3.up);         
         }
Comment
Add comment · Show 1
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 Baste · Nov 17, 2015 at 02:31 PM 1
Share

That'll only work if the selected parts are not rotated in any way.

I'd just add a component to each part, with a variable on it that gives the rotation the car needs to be in to view that part. The angle you need to have to look at the mirror is very different from the angle you need to have to look at a spoiler or a wheel.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KEric · Nov 21, 2015 at 12:27 PM

The problem with the above code was in returned value of Vector3.Angle function as it returns an angle of a positive value (0, 360) whereas in the method I came up above I needed a value from (-180, 180) scope. Below I'm attaching working snipped of code

     // class ...
     void Update ()
     {   
         transform.rotation = Quaternion.Lerp(transform.rotation, q, Time.deltaTime);
     }
 
     public PartUpgrade Selection
     {
         set
         {
              selection = value.transform;
 
               Vector3 camFwd = -Camera.main.transform.forward; // camera fwd always (0, 0, -1)
               Vector3 selFwd = new Vector3(selection.forward.x, 0, selection.forward.z).normalized;
 
               angle = Vector3.Angle(camFwd, selFwd) * 
                  Mathf.Sign(Vector3.Dot(-Vector3.up, Vector3.Cross(camFwd, selFwd)));
                     
                q = transform.rotation * Quaternion.AngleAxis(angle, Vector3.up);
         }
     }
Comment
Add comment · 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

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

43 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can anyone clearly explain what Quaternion.angleaxis is 2 Answers

How can I rotate my player with a Joystick? 0 Answers

Rotate object to target position 1 Answer

Rotate object based on hit normal + matching camera direction 0 Answers

[Unity 3D] How to detect if the game object is left right from the player? 0 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