• 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 Neilos · Jun 22, 2015 at 03:42 PM · cameramath

Camera rotation using mouse

I have tried to implement a basic camera movement around a third person character which is controlled by the mouse, however I get strange behavior and it's been a while since I did this maths.

I have a camera it is not a child of the character the camera is supposed to follow the character but rotate relative to the scene. Therefore rotating the character should not affect the camera and vice-versa.

Here is what I have got so far (`gameObject` is the camera):

 public void Update() {
     float yawAmount = Input.GetAxis ("Mouse X");
     float pitchAmount = Input.GetAxis ("Mouse Y");
     RotateCamera(yawAmount, -pitchAmount);

     // position the camera
     gameObject.transform.position = (camLocalPos + curTarget);

     // look at target
     gameObject.transform.LookAt (curTarget);
 }

 public void RotateCamera(float x, float y) {
     yaw += x*0.1f;
     pitch += y*0.1f;
     if (pitch > maxPitch)
         pitch = maxPitch;
     if (pitch < minPitch)
         pitch = minPitch;
     UpdateCamera ();
 }

 private void UpdateCamera() {
     camLocalPos.x = distance * Mathf.Cos(pitch) * Mathf.Sin(yaw);
     camLocalPos.y = distance * Mathf.Sin(pitch) * Mathf.Sin(yaw);
     camLocalPos.z = distance * Mathf.Cos(yaw);
 }

It mostly works, but depending on which way the camera is facing it flips the pitch such that looking one way up is up and looking the other way up is down :/ (and everything in between).

Can anyone point out the error please?

Update

I realised that I had the pitch and yaw the wrong way round it should be:

     camLocalPos.x = distance * Mathf.Cos(yaw) * Mathf.Sin(pitch);
     camLocalPos.y = distance * Mathf.Sin(yaw) * Mathf.Sin(pitch);
     camLocalPos.z = distance * Mathf.Cos(pitch);

but changing this makes it worse! I'm 99% sure that the error is in the above three lines but I have been over and over it and cannot see anything wrong:

alt text

alt text

alt text

alt text

Update 2

Are the axes in the correct orientation?

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
1
Best Answer

Answer by Eno-Khaon · Jun 23, 2015 at 10:19 AM

Okay, I think I see the problem here.

To answer your second update, no, the axes DO NOT appear to be in the correct orientation. From what you've described, your resultant view is primarily top-down in world space, rather than perpendicular to the world up/down on the Y-axis.

Why does this matter? LookAt() is based around the Unity's world coordinate system, in which X runs left to right, Y runs bottom to top, and Z runs back to front. By default, Lookat() assumes that the camera should look at its target where "up" corresponds to the global "up" direction.

The problem you're encountering based on your current script is known as Gimbal Lock. In short, it occurs as a result of Euler angles (0-360 degrees on X, Y, and Z axes) having multiple ways of describing the same rotation. You can either rotate an object 180 degrees on the Y axis, or you can get an identical result by rotating both the X and Z axes the same way. Unity doesn't automatically know what you're trying to accomplish, so it simply guesses.

Well, this same principle applies when you cross over any axis boundary. To support as much sensible, logical circumstance as possible, the Y-axis is left as the only ambiguous line to cross. As a result, looking any further than straight up or straight down will not cope with Lookat() in an expected manner in most circumstances. This is primarily a problem for you because you're misappropriating the axes and basically have turned the world on its side, resulting in an unintentionally easy crossing over the Y-axis which, again, is mainly only a problem for LookAt() in this situation.

To summarize, Unity's axis alignment is one in which X and Y are a 2-dimensional left/right/down/up. The Z axis adds a third dimension from back to front, giving Unity a Left-Handed Coordinate System.

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 Neilos · Jun 23, 2015 at 10:36 AM 0
Share

I think we had the same thought, just that you elaborated much more. I thought I had the axes messed up but I was at work and unable to check it. It would seem that you are right and that the y and the z values are not correct (I think that because I had it this way I played about with changing the pitch and yaw which gave a closer appearance even though the code was more wrong). So I surmise that y=r*cos(theta) and z=r*sin(theta)*sin(phi), I'll check it later and report back if this is incorrect (which I think it is not).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rotating camera around the sphere 2 Answers

Convert screenspace coordinates (top-left origin) to worldspace coordinates. Read before you react! 1 Answer

touch input speed to camera rotate speed. scaling function needed 0 Answers

Transform.position of child object not working as intended. 1 Answer

instantiate prefab directly off camera 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