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
1
Question by Caleb_1701 · Oct 08, 2015 at 01:45 AM · camerarotationfpscamera rotateconstraints

FPS Camera Control rotation limits

I am kind of new to Unity and don't have a lot of code experience. I've been working on an FPS game and have written the following code:

 using UnityEngine;
 using System.Collections;
 
 public class fps_control : MonoBehaviour {
     public Rigidbody otherRb;
     public float speed;
     public float turn;
     public float limit_up;
     public float limit_down;
     public GameObject parentObj;
 
     private float rot;
 
     void Awake () {
         Cursor.lockState = CursorLockMode.Locked;
         otherRb = parentObj.GetComponent <Rigidbody> ();
         float xstart = Input.mousePosition.x;
         float ystart = Input.mousePosition.y;
     }
     void FixedUpdate () {
         float xturn = Input.GetAxis("Mouse X");
         float yturn = Input.GetAxis ("Mouse Y");
         parentObj.transform.Rotate (0f, xturn * turn, 0f);
         if (transform.rotation.x <= limit_up) {
             if (transform.rotation.x >=limit_down) {
                 transform.Rotate (-yturn * turn, 0f, 0f);
             }
             else {
                 rot = limit_down;
                 transform.rotation = Quaternion.Euler (rot,0f,0f);
             }
         }
         else {
             rot = limit_up;
             transform.rotation = Quaternion.Euler (rot,0f,0f);
         }
     }
 }

The problem is that the rotation limits I tried to set for the camera aren't working (resulting in the character doing some crazy somersaults). What am I doing wrong? Please help, and thank you to anyone who answers (even if your answer is crap... I still appreciate your effort).

Comment
Add comment · Show 6
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 landon912 · Oct 08, 2015 at 01:45 AM 0
Share

I've approved in the hopes that'll you'd edit your post to include relevant code and clean up the formatting.

avatar image Cherno landon912 · Oct 08, 2015 at 08:56 AM 0
Share

The Standard Assets package has a $$anonymous$$ouseLook script that uses rotation limits.

avatar image Caleb_1701 Cherno · Oct 08, 2015 at 10:00 PM 0
Share

I looked into it, but I think it would be better if I just tried to find the problem with this script. I'm sure that the Standard Assets $$anonymous$$ouseLook script is great, but if I don't find out what's wrong with this script, then I'll likely encounter other problems in the future that can't be solved just by downloading something from the Assets Store.

If I have to, though, I may try that script. Thank you for responding.

Show more comments
avatar image maccabbe · Oct 09, 2015 at 02:39 AM 0
Share

The first problem I see is that you are using transform.rotation.x which is a Quaternion (w, x, y, z) while you probably want to use transform.eulerAngles.x.

However you have a deeper problem when you assume that setting the rotation of a transform to (x, y, z) will return a rotation of (x, y, z). Unfortunately multiple (x, y, z) combinations can actually represent the same rotation which can lead to weird results, for instance (90, 0, 0) and (90, 180, 180) are the same rotation. If you want to store a euler rotation then use a Vector3 in your class ins$$anonymous$$d of relying on the Quaternion transform.rotation to preserve all the data. Then you can assign transform.eulerAngles = storedEulerAngles while replacing things like

 Vector3 eulerRotation = transform.eulerAngles;

and

 if(transform.eulerAngles.x<0)

with

 Vector3 eulerRotation = storedEulerAngles ;

and

 if(storedEulerAngles .x<0)
avatar image Caleb_1701 maccabbe · Oct 09, 2015 at 10:37 PM 0
Share

Well, I got it working. Thanks for your answer! However, there are 2 new (but fortunately smaller) problems. The first one is that I have to choose really weird numbers for the upper and lower limits or else the camera gets stuck. The other one is that the camera seems to vibrate when it's at the upper or lower limits. Here is the updated code:

 using UnityEngine;
 using System.Collections;
 
 public class fps_control : $$anonymous$$onoBehaviour {
     public Rigidbody otherRb;
     public float speed;
     public float turn;
     public float limit_up;
     public float limit_down;
     public GameObject parentObj;
     
     private Vector3 angles;
 
     void Awake () {
         Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
         otherRb = parentObj.GetComponent <Rigidbody> ();
         float xstart = Input.mousePosition.x;
         float ystart = Input.mousePosition.y;
     }
     void FixedUpdate () {
         float xturn = Input.GetAxis("$$anonymous$$ouse X");
         float yturn = Input.GetAxis ("$$anonymous$$ouse Y");
         parentObj.transform.Rotate (0f, xturn * turn, 0f);
         if (angles.x <= limit_up) {
                 transform.Rotate (-yturn * turn, 0f, 0f);
             }
         else if (angles.x >=limit_down) {
             transform.Rotate (-yturn * turn, 0f, 0f);
         }
         else if (angles.x < 180) {
             angles.x = limit_up;
             transform.localEulerAngles= angles;
         }
         else {
             angles.x = limit_down;
             transform.localEulerAngles = angles;
         }
         angles = transform.localEulerAngles;
 
     }
 }
 

What exactly am I doing wrong? It's better than before, but it does need to be fixed.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Caleb_1701 · Mar 30, 2016 at 12:02 AM

For anyone wanting to see the answer to this question, look in the comments. I'm only posting this to close the question, because maccabbe's answer was posted in the comments instead of here.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I properly rotate an FPS camera with a joystick? 1 Answer

Forward and back movements with a camera emulating an isometric view 1 Answer

Mouse axes based on position, not movement. 1 Answer

How Do i Prevent The First Person Camera From Turning 0 Answers

Camera rotates when I run into a wall 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