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 /
  • Help Room /
avatar image
0
Question by Tegomlee · Dec 29, 2016 at 02:19 PM · camerarotationfps

How can I stop my FPS camera from rotating 360 degrees?

Hello everyone, I am pretty new to coding in general and even more so in Unity. I am making an fps game and I would to limit the rotation up and down. how can I accomplish this?

here is my FPSCamera Script:

 private float xRotate, yRotate;
 private Quaternion cameraQuaternion;

 void FixedUpdate()
 {
     cameraQuaternion = Quaternion.Euler (0, xRotate, 0);
     GameObject.Find ("Player").transform.rotation = cameraQuaternion;
     xRotate += Input.GetAxis ("Mouse X");
     yRotate += Input.GetAxis ("Mouse Y");
     transform.rotation = Quaternion.Euler (yRotate,xRotate,0);
 }
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
Best Answer

Answer by ScaniX · Dec 29, 2016 at 03:34 PM

You could simply use Min/Max:

 private GameObject player;
 private float xRotate, yRotate;
 
 void Start() {
     player = GameObject.Find("Player");
 }
 
 void Update()
 {
      xRotate = Mathf.Min(50, Mathf.Max(-50, xRotate + Input.GetAxis ("Mouse X")));
      yRotate += Input.GetAxis ("Mouse Y");
      player.transform.localRotation = Quaternion.Euler(0, xRotate, 0);
      transform.localRotation = Quaternion.Euler(yRotate, 0, 0);
 }

Some notes:

  • Using localRotation is probably looking better, assuming your camera is a child of the player.

  • You should get the player once in the Start() method or assign it through the editor instead of calling Find() each frame.

  • Inputs should be processed in the Update(), not the FixedUpdate() as they are cleared per frame.

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 Tegomlee · Dec 29, 2016 at 03:55 PM 0
Share

Thank you very much for responding. It worked excepted I had to change the xRotate line to the yRotate line and vice versa. $$anonymous$$any thanks though, very appreciated. private GameObject player; private float xRotate, yRotate;

  void Start()
  {
       player = GameObject.Find("Player");
  }

  void Update()
  {
       xRotate += Input.GetAxis("$$anonymous$$ouse X");
       yRotate = $$anonymous$$athf.$$anonymous$$in(50, $$anonymous$$athf.$$anonymous$$ax(-50, yRotate + Input.GetAxis("$$anonymous$$ouse Y")));
       player.transform.localRotation = Quaternion.Euler(0, xRotate, 0);
       transform.localRotation = Quaternion.Euler(yRotate, 0, 0);
  }
avatar image ScaniX Tegomlee · Dec 29, 2016 at 03:57 PM 0
Share

You're welcome!

I see my mistake. You are using xRotate for the Y-rotation and yRotate for the X-rotation, that must have confused me. ;)

avatar image Tegomlee · Dec 29, 2016 at 03:58 PM 0
Share

On an unrelated topic, is there any website/forums where I can post my scripts and have people give me pointers on how I can improve the script.

avatar image ScaniX Tegomlee · Dec 29, 2016 at 04:19 PM 0
Share

Did you check out the unity forums?

Answers is a place to ask for solutions to specific problems, so this is not really the right place. Unfortunately I don't know of any other place for this (as I'm pretty shy when it comes to showing my code around ^^).

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

125 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

Related Questions

How to rotate camera in FPS with joystick 4 Answers

How to bring UI element infront of other objects? 0 Answers

Transform.Rotate x and y rotate z, where z is 0 0 Answers

Cut viewport Rect of the camera in half 0 Answers

Rotate camera upward. 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