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 erwol · May 12, 2017 at 01:15 PM · camera-movementcamera rotaterotatearoundrotate-around

Rotate camera around pivot not working on certain angles

Hello, this's my first post here; I hope to ask my question properly.

In a scene I'm creating I want the user to be able to rotate and tilt his camera (main one) around the point where he has made a left click while he's pressing the left click button. So I get the point where the user has started to press with this function:

 void GetPivot()
     {
         Plane plane = new Plane(Vector3.up, Vector3.zero);
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         float rayDistance;
         if (plane.Raycast(ray, out rayDistance))
         {
             pivotPoint = ray.GetPoint(rayDistance); // This's a vector3
         }
     }

It works fine. The pivot point's set just once (everytime the user press the left button) thanks to this piece of code that I wrote inside the Update method:

         if (Input.GetMouseButton(0))
         {
             if (!helding)
             {
                 GetPivot();
                 helding = true;
             }
             RotateAroundPivot();
         }
         else
         {
             Cursor.visible = true;
             helding = false;
         }

After I've the pivot point, Update calls the RotateAroundPivot method that looks like this:

 private void RotateAroundPivot()
 {
     float pivotSpeed = 200;
 
     float horizontalMovement = Input.GetAxis("Mouse X") * pivotSpeed;
     transform.RotateAround(pivotPoint, Vector3.up, -horizontalMovement * Time.deltaTime);
 
     float verticalMovement = Input.GetAxis("Mouse Y") * (pivotSpeed / 2);
     transform.RotateAround(pivotPoint, Vector3.right, verticalMovement * Time.deltaTime);
  }
 


And then the problems arises. While the rotateAround with Vector3.up works like a charm: alt text

*The red cube location equals to the pivot location; it's just for clarity.

... rotateAround with Vector3.right as pivot only works on certain positions: alt text

Basically it only only works fine in the initial camera position that, by the way, it's:

 public Vector3 defaultCameraPosition = new Vector3(0, 11, -24);
 public Vector3 defaultCameraRotation = new Vector3(25, 0, 0);

It also works fine in the opposite side.

I guess this's quite of a noob question, related to the angles control that I should be doing. I noticed that the solution might be in controlling the y camera rotation angle. It seems to be working in the [315º-360º] and [0º-45º]... but I don't fully onderstand why. I really hope someone overhere can give me an explanation and luckily an answer.

Thank you very much.

fail.gif (323.2 kB)
ok.gif (437.7 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

0 Replies

· Add your reply
  • Sort: 

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

105 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

Related Questions

how to jump my character to fixed distance 0 Answers

How to rotate a camera around a spaceship freely 0 Answers

Top-Down Camera view (with rotation) need help (third person script) 0 Answers

Limit Rotate Around axis 0 Answers

C# Smooth Follow Space Ship 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