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 unity_vJeO3Q91KqSEsg · Jul 20, 2018 at 08:18 PM · rotationrotation axisscriptable objectcalculationrelative rotation

Rotate an object's yaw, pitch, and roll relative to the Game Camera Axis.

Hi Everyone!

I'm currently trying to write a script to rotate an object relative to the camera's reference axis. As of now in the script below, objects are rotated relative to themselves. I would like to change this, see below (this is a script attached to the gameobject to be rotated which references a control script and the camera transform).

  void Rotate(float deg)
         {
             // Make sure the camera exists
             var camera = GetCamera(Camera, gameObject);
 
             if (camera != null)
             {
                 int rotMode =obj.GetComponent<AssetControl>().RotationMode;
                 var axis = new Vector3(0, 0, 0);
                 if (rotMode == YAW) {
                     axis = new Vector3(0, 1, 0); //rotate about the y axis
                 } else if (rotMode == ROLL) {
                     axis = new Vector3(1, 0, 0); //rotate about the x axis
                 } else if (rotMode == PITCH) {
                     axis = new Vector3(0, 0, 1); //rotate about the z axis
                 }
                 transform.rotation *= Quaternion.AngleAxis(deg, axis); //do rotation
             }
         }

Ideally I would like to rotate the object relative to the camera orientation, i.e. if rotating by ROLL, rotate the the object about the axis pointing the the camera.x direction, rather than the x axis of the global world. In a nut shell, I'm trying to replace the game objects coordinate frame (x,y,z arrows), with the cameras coordinate frame, and rotate the about about itself. Basically there's no translation, and I'm not trying to rotate the object around the camera. I'm trying to rotate the object in place around the x,y, or z axis individual but with the reference of the camera's orientation (sorry for being redundant, I'm just trying to specify my problem in more detail).

Does anyone know how to do this? I tried to use the code below, but it did not work...

 //get direction vector of camera to object and treat it as global axis of rotation
             Vector3 dirVect = this.transform.position - camera.transform.position;
             dirVect = Vector3.Scale(dirVect, new Vector3(1, 0, 1));
             dirVect = dirVect.normalized;  //dirVect now becomes direction of camera x axis?

I'd appreciate any help! Thank you for your time!!!

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 Nomenokes · Jul 20, 2018 at 08:27 PM 0
Share

I don't have an exact answer for you, but I find it useful to temporarily make the object a child of the camera so you can use Unity's built-in relative rotation functions, then immediately un-child the object. Such temporary transform flips aren't a big drag on performance.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JVene · Jul 20, 2018 at 09:39 PM

Referencing Unity's Quaternion Documentation try something like:

 Quaternion q = Quaternion.Euler( degxaxis, degyaxis, degzaxis );
 
 q = camera.transform.rotation * q;
 
 transform.rotation = transform.rotation * q; 

My 3rd coffee hasn't been served, so think upon these lines as I'm not checking anything here. The idea is to fashion a Quaternion for the rotation on whatever axis you intend, then transforming that rotation in the camera's orientation, then applying the result to the object's rotation. I've not taken into account any selection of localRotation vs rotation, or any implication about mirroring, I haven't considered the order of multiplication (reversing the order produces different results), and I'm assuming the camera is looking directly at the object in question.

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

121 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 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 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

Rotating On Two Axes Independantly 0 Answers

Strange rotation pattern. 0 Answers

Rotate cube non-relative to current rotation. 1 Answer

Problem when i rotate a 2d object aroun Z-axis 1 Answer

How do you make a Tps cam with RotateAround around the X and Y axis without rotating on the Z axis? 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