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 rageingnonsense · Feb 09, 2016 at 09:12 PM · rotationrotation axisquaternions

Quaternions; how do I rotate an object around only 2 axis based on another rotation?

Cross posted from the forums:

So I have a camera that rotates around an object, always facing it. What I would like to do now is to have that object rotate with the camera, but only along the xz plane (in other words; around the y axis).

Imagine a skyscraper with a camera rotating around it in all directions, always facing it. I want the skyscraper to always face the camera, but not leave the ground; as if it were rotating on a turntable.

I don't have much code so far, but this is what I have tried:

             Quaternion difference = Quaternion.Inverse(cameraRigging.rotation) * masterCamera.transform.rotation;
  
             // rotate camera reletive to main camera
             cameraRigging.rotation = masterCamera.transform.rotation;
            
             // rotate the object such that it is still facing the
             // same direction reletivly to the camera along z and
             // x axes (NOT WORKING)
             target.transform.rotation *= difference;


This isn't really working; the rotation doesn't stay on Y (as expected), but it also is not rotating the way I would expect. Target almost faces the same direction as camera, but not quite.

I've also tried LookRotation and SetLookRotation, but could not get those to work either.

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 toddisarockstar · Feb 09, 2016 at 11:18 PM 0
Share

i would start by making the character object the parent of the camera and take it from there.

another words, drag and drop the camera object on to the character object in the hierarchy in the inspector. this way it is "stuck" to the character.

you can still easily adjust any of you other custom movements after parented.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by JoshuaMcKenzie · Feb 10, 2016 at 04:17 AM

if you want to rotate by only the xz axis you can follow these steps:

  1. find the direction between the camera and the skyscraper

  2. zero the y value out

  3. apply it to the Skyscraper's Look rotation

      // find the direction
         Vector3 direction = Camera.main.transform.position - transform.position;
         
         //zero out the y value
         direction.y = 0;
         
         // set the skyscraper's look rotation to this value
         transform.rotation = Quaternion.LookRotation(direction,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

42 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

Related Questions

need my object to rotate between specific angles on the x axis 2 Answers

Prevent child rotation but still control it yourself 1 Answer

Rotating 2D sprite to match surface. 0 Answers

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