Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jaja1 · Dec 11, 2014 at 04:33 AM · c#camerarotation

Problem with Camera rotation script

Trying to make an all round view camera by using the mouse right click button. Some what similar to the unity editors view. I am having problems with the rotation about vectors parallel to the xz plane however. My character stands parallel to the Y axis. so I tried to get a Vector that would always be parallel to the xz plane yet perpendicular to the direction the camera is facing. However I seem to be getting a vector pointing somewhere off to the left corner of my screen (camera is facing player from the back and slightly angled downward). Can anyone explain what I am doing wrong? Note: I commented out the rotation about the Y-axis just for testing purposes.

         //rotate camera from mouse input
         if (Input.GetMouseButton(1))
         {
             float X = Input.GetAxis("Mouse X");
             float Y = Input.GetAxis("Mouse Y");
             /*
             //adjust y rotation
             if (X > 0)
             {
                 transform.RotateAround(transform.parent.localPosition, Vector3.up, -100f * Time.deltaTime);
             }
             else if (X < 0)
             {
                 transform.RotateAround(transform.parent.localPosition, Vector3.up, 100f * Time.deltaTime);
             }
             */
             //adjust x-z rotation
             if (Y > 0)
             {
                 Vector3 rotAround = Vector3.Cross(transform.forward, Vector3.up);
                 transform.rotation = transform.rotation * Quaternion.AngleAxis(100f * Time.deltaTime, rotAround);
             }
             else if (Y < 0)
             {
                 Vector3 rotAround = Vector3.Cross(transform.forward, Vector3.up);
                 transform.rotation = transform.rotation * Quaternion.AngleAxis(-100f * Time.deltaTime, rotAround);
             }
         }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Ijatsu · Dec 11, 2014 at 07:14 AM

You should use Vector3.left instead of "rotAround".

When you multiply quaternions, the second quaternion will apply the rotation considering the left quaternion as "origin", so it'll basically rotate your character on its local X axis no matter its Y axis rotation.

What you're doing here is that you get the perpendicular vector of the plane designed by Vector3.up AND the LOCAL forward vector of your transform. Meaning that this wont be Vector3.left but something already relative to the Y rotation of your character.

So if you then mult rotAround considering your character's origin, it won't rotate it to the relative X axis of your character, but on something else.

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
avatar image
1

Answer by FairGamesProductions · Dec 11, 2014 at 11:09 AM

If I am understanding your goal correctly (rotate your camera around the Y axis while Right Click is pressed), then why not do something a LOT simpler (js):

 var MainCamera : GameObject;
 var Sensitivity = 1.0;
 
 function Update ()
     {
     if (Input.GetMouseButton(1))
         {
         MainCamera.transform.rotation.eulerAngles.y += (Input.GetAxis("Mouse X") * Sensitivity);
         }
     }

Then play around with the sensitivity a little to get it right, and like this you can also offer the camera sensitivity as an option in the Menu.

EDIT: And you can do the same for the X axis, if you wanna give an up/down control too, only use "Mouse Y" axis for that.

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
avatar image
1

Answer by jaja1 · Dec 11, 2014 at 07:34 PM

Thanks @FairGamesProductions and @Ijatsu Both of your methods are valid and work. However I found @FairGamesProductions 's method to be a bit tedious in C#. I have to assign temporary variables and that sort of thing in C# as opposed to Java (unless that was just pseudo code, sorry I haven't used java in ages).

I managed to find another method specifically for rotating about the x axis. However its not so much of a rotation but more of a translation along the vertical axis and having the camera to look at the player (which is essentially the rotation part). It seems a little unconventional but it gave better results for MY game. It may not work for everyone. Anyway here it is:

  if (Y > 0 && transform.localPosition.y <= 4.5f)
             {
                 //transform.rotation = transform.rotation * Quaternion.AngleAxis(100f * Time.deltaTime, Vector3.left);
                 transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y - (4f * Time.deltaTime), transform.localPosition.z);
                 transform.LookAt(transform.parent.localPosition);
             }
             else if (Y < 0 && transform.localPosition.y >= 1f)
             {
                 //transform.rotation = transform.rotation * Quaternion.AngleAxis(-100f * Time.deltaTime, Vector3.left);
                 transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y + (4f * Time.deltaTime), transform.localPosition.z);
                 transform.LookAt(transform.parent.localPosition);
             }
Comment
Add comment · Show 4 · 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 Ijatsu · Dec 11, 2014 at 08:21 PM 0
Share

You also can simplify

  transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y + (4f * Time.deltaTime), transform.localPosition.z);


to

  transform.localPosition.y += (4f * Time.deltaTime);

$$anonymous$$ake sure you don't have an unattended behavior when none of your conditions are true (here does nothing).

avatar image FairGamesProductions · Dec 11, 2014 at 09:07 PM 0
Share

O$$anonymous$$, then why not use JS for the camera movement, if it's A LOT simpler in JS?

avatar image Ijatsu · Dec 11, 2014 at 09:11 PM 0
Share

It's not a lot simpler, it's the same, just use what you're the most comfortable with.

avatar image FairGamesProductions · Dec 11, 2014 at 09:19 PM 0
Share

Whatever works for your specific needs jaja1

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

27 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

Related Questions

Flip over an object (smooth transition) 3 Answers

How to make an object face the mouse in a non-2D world 1 Answer

RTS Camera movement wrong after rotation 1 Answer

move the object where camera look 0 Answers

help with rotation, mouse input and time.deltatime, a thirdperson script 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