• 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.

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)
Show more comments

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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

First Person Camera looking down 1 Answer

Camera movement and gun follow in unity 1 Answer

Camera rotation on z axis 2 Answers

[Solved :D] Camera auto turns 180° from its init position on Play (Animated GIF Included!) 1 Answer

Reset player movement axes after camera rotation 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