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
1
Question by RoyalJellyfish · Dec 23, 2012 at 07:26 PM · camerafps

FPS camera script - keeps reverting to 0,0,0 rotation

Hi. I've been trying to make a script for a First Person camera, and I've looked around but I can't seem to find anything that someone else has made that I can incorporate as I want. The problem I'm having is that in my script, whenever I move my mouse the rotationX and rotationY seem to keep jumping back to 0 immediately after the value changes (I can see the camera move very quickly before it goes back to 0 rotation). I kind of tried to simplify other examples of FPS scripts I've seen, but what could be missing (what is there that shouldn't be there?) that's making my variables go back to 0?

 var sensitivityX:float = 15f;
 var sensitivityY:float = 15f;
 var rotationX:float = 0f;
 var rotationY:float = 0f;
 var originalrotation:Quaternion;
 
 function start ()
 {
     originalrotation = transform.localrotation;
 }
 
 function Update ()
 {
     rotationX = Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime;
     rotationY = Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
     xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
     yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.right);
     transform.localRotation = xQuaternion * yQuaternion;
 }
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 aldonaletto · Dec 23, 2012 at 07:55 PM

GetAxis("Mouse X/Y") functions return the mouse movement in the corresponding axis since last frame, thus you must accumulate them to get usable rotations - and since the mouse movement is already proportional to the time between frames, multiplying by Time.deltaTime isn't needed. Additionally, you should limit rotationX and rotationY to avoid other problems: rotationX must be in modulo 360 to avoid range issues with AngleAxis, and rotationY must be clamped to a range smaller than +/-90 degrees to avoid camera madness:

 rotationX += Input.GetAxis("Mouse X") * sensitivityX;
 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
 rotationX = rotationX % 360;
 rotationY = Mathf.Clamp(rotationY, -80, 80);

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 RoyalJellyfish · Dec 23, 2012 at 10:52 PM 0
Share

Aaagh, such a simple correction XD. Thanks! I believe that I won't be using clams due to the nature of how I plan of using the camera's view, but in case I need it that'll sure help too :D!

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

10 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

Related Questions

I need help with my script 3 Answers

Camera Height-based crouch script 2 Answers

how can i make my character jump? 5 Answers

Camera Bobbing and Stairs 4 Answers

I want to change gun position with my camera joystick 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