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 /
  • Help Room /
avatar image
0
Question by No_Username_Found · Nov 27, 2016 at 06:28 PM · lookatrotatearound

Transform.LookAt() causes spinning

The code is simple. On a quad (named character), I have this code:

     // Update is called once per frame
     void Update () {
 
         //look away from camera so that th token picture is always 'flat' towards the camera
         transform.LookAt (transform.position - mCamera.position, transform.up);
     }

And on a camera (mCamera) I have this code:

 if (Input.GetMouseButton (2)){
     //        Debug.Log ("Middle Clicked.");
 
 
             //mouse moving also?
             if (Input.GetAxis ("Mouse X") > 0 ){
                 if (Input.GetKey(KeyCode.LeftControl)){
                     transform.RotateAround (character.position, -transform.up, rotateSpeed * Time.deltaTime);
                 }
             }
             else if (Input.GetAxis ("Mouse X") <0){
                 if (Input.GetKey (KeyCode.LeftControl)){
                     transform.RotateAround (character.position, transform.up, rotateSpeed * Time.deltaTime);
                 }
             }/**/
 
 
             if (Input.GetAxis ("Mouse Y") < 0){
                 if (Input.GetKey (KeyCode.LeftAlt)){
                     transform.RotateAround (character.position, transform.right, rotateSpeed * Time.deltaTime);
                 }
             }
             else if (Input.GetAxis ("Mouse Y") > 0){
                 if (Input.GetKey (KeyCode.LeftAlt)){
                     transform.RotateAround (character.position, -transform.right, rotateSpeed * Time.deltaTime);
                 }
             }/**/
         }

The idea is that the quad (character) is always centered in the camera (mCamera) as the camera rotates 3d. The quad always faces "away" from the camera so that it looks flat within the 3d environment.

The problem is that when I move the camera Y down so that the camera looks at the horizon, then rotate the X left or right the quad starts to spin very slowly. If I take out the "transform.up" from LookAt() the spinning is much faster.

How do I stop the spinning completely?

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 Bunny83 · Nov 27, 2016 at 07:27 PM

You have several problems here. The most important one is that "LookAt", as the name suggest, wants "something" to look at. However you passed a relative direction vector instead. So the point you're looking at is just around the world origin since your vector is taken as world space position.

If you want to use a direction vector you have to use Quaternion.LookRotation(fwd, up);. It does basically the same thing but takes two direction vectors instead of a worldspace position and a direction.

 transform.rotation = Quaternion.LookRotation(transform.position - mCamera.position, transform.up);

Next thing is Input.GetAxis returns the amout the mouse has moved that frame. You usually want to use that value as the amount you want to rotate this frame. So your code can even be simplified to:

 if (Input.GetMouseButton (2))
 {
     if (Input.GetKey(KeyCode.LeftControl))
     {
         transform.RotateAround (character.position, transform.up, -Input.GetAxis ("Mouse X") * rotateSpeed);
     }
     if (Input.GetKey(KeyCode.LeftAlt))
     {
         transform.RotateAround (character.position, transform.right, -Input.GetAxis ("Mouse Y") * rotateSpeed);
     }
 }

Though RotateAround is a bad choice for such a control as it applies a relative cirular motion to the camera object so over time it might "drift" due to floating point precision.

Since you rotate your plane object exaclt in the same direction the camera is facing, wouldn't it be much easier to just rotate the object directly and have the camera as child object behind the object? That way the camera always views the object from the same angle.

Most independent camera orbiting scripts usually use absolute rotation values (either twi eulerangles or a quaternion) and place the camera based on the rotation at a specified distance behind the object.

 public Transform target;
 public float distance = 5f;
 
 void Update()
 {
     if (Input.GetMouseButton (2))
     {
         if (Input.GetKey(KeyCode.LeftControl))
         {
             transsform.rotation = transsform.rotation * Quaternion.AngleAxis(-Input.GetAxis ("Mouse X") * rotateSpeed, transform.up);
         }
         if (Input.GetKey(KeyCode.LeftAlt))
         {
             transsform.rotation = transsform.rotation * Quaternion.AngleAxis(-Input.GetAxis ("Mouse Y") * rotateSpeed, transform.right);
         }
         transform.position = target.position -transform.forward*distance;
     }
 }

If you don't want to specify the distance in the inspector but instead use the initial distance you can add this in Start:

 void Start()
 {
     distance = Vector3.Distance(transform.position, target.position);
 }

Comment
Add comment · Show 1 · 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 No_Username_Found · Nov 28, 2016 at 01:03 AM 0
Share

Thank you for the very detailed answer.

I am now using the Quaternion.LookRotation() and it has fixed the issue with the quad spinning while rotating. I very much appreciate your prompt reply.

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

58 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

Related Questions

Best Method to Rotate Camera around a GameObject while looking at another? 0 Answers

rotate Y axis towards position 1 Answer

Creating a Window - Camera movement 0 Answers

Error: player.position not part of UnityEngine.GameObject 1 Answer

look at closer 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