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 xNIkurasu · Oct 22, 2018 at 04:45 PM · camerafpsmouselookfps controllerlocalrotation

localRotation rotates by 180°

Hello! Im currently making an FPS and Im currently trying to keep the moment of my jump, but i want to be able to look around freely. I have an object (FPSController) which is the object that controls the movement and a child object (CameraRotation) which has the camera as a child object, so i can rotate my camera with the CameraRotation object. I can now look around the y-axis, but somehow i cant get the rotation of the x axis going:

 else if(!isGrounded)
         {
             fpsCam.transform.localEulerAngles = new Vector3(y, 0, 0);
             var xRot = Input.GetAxisRaw("Mouse X") * Time.deltaTime * lookSpeed;
             fpsCam.transform.localRotation = new Quaternion(0,xRot,0,0);
         }

With this code, my camera rotates by 180° everytime i move my mouse to the left or to the right. What I did could be complete nonsense, but Im still new to Unity so please excuse my sloppy coding. I dont expect you to provide code or anything, just simple instructions on how to fix the problem are more than enough.

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

Answer by JVene · Oct 22, 2018 at 05:30 PM

There is a lot one could say about what is wrong here, but let's start with the last line because that leads to an understanding that fixes everything else.

It appears you're familiar with Euler angles (your first line in the code block), but when you fashion a Quaternion (in the last line), you're fashioning the Quaternion with 4 parameters, and they are not Euler angles. Nothing in the constructor of a Quaternion will make much sense if you're not really familiar with the mathematics of quaternions (the subject of math beyond that of Unity). A quaternion (and thus Unity's Quaternion class) represents a complex number, like the imaginary numbers taught in algebra whereby the square root of a negative number becomes possible. The complex numbers taught in algebra are generally related to a 2 dimensional representation (like the imaginary numbers which have two values, an imaginary part and a real part). Quaternions, however, are complex numbers in 3 dimensions. None of the 4 parameters (x,y,z,w) have any meaning relative to rotations in Euler angles. This is the problem you're having. You are providing a value you intend to interpret as an angle in degrees, but the y parameter to the Quaternion constructor has no relationship to such an angle in the y axis.


To fix that, and therefore propagate that upwards to the rest of your code, look up the static function Quaternion.Euler, which forms a correct Quaternion object given Euler angles in 3 axis, as you're expecting. You could also consider using the eulerAngles property of localRotation as an alternative approach, or at the least in your translation between any quaternion and an orientation in 3 axis.

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 xNIkurasu · Oct 22, 2018 at 07:31 PM 0
Share

Hello! Thanks for the answer! Sadly, that only partly fixed my problem. $$anonymous$$y camera does not rotate by 180° anymore, which is a good thing, but now i cant really move my camera on the x-axis anymore. It looks like its being forced back to the rotation of the parent object. I understand that child objects inherit the position and rotation of the parent object, but i was hoping i could use localRotation, but, for some reason, it only works on the y-axis. Do you perhaps have any idea why that is the case? Thanks again for the help.

avatar image JVene xNIkurasu · Oct 22, 2018 at 08:52 PM 0
Share

I'd need to see your updated code, because in the example you've posted you only request the X axis from the mouse.

avatar image xNIkurasu JVene · Oct 22, 2018 at 09:33 PM 0
Share

Oh yeah my bad:

 else if(!isGrounded)
         {
             var xRot = Input.GetAxisRaw("$$anonymous$$ouse X") * Time.deltaTime * lookSpeed;
             fpsCam.transform.localRotation = Quaternion.Euler(y,xRot,0);
         }
Show more comments

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

178 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 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

MouseLook won't work in Windows (Parallels) 3 Answers

Switching to the player camera 1 Answer

how to make my bones follow the camera(FPS) 0 Answers

Problems with Dani's Karlson FPS Movement Controller 1 Answer

FPS 3D camera view problem 0 Answers


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