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
0
Question by sephiroth111 · Oct 10, 2012 at 02:58 PM · c#clamp

Limiting the rotation of x-axis

Hello to all, I.m here to ask something about limiting the rotation of the child camera from x axis. i tried adding the clamp inside the Rotate() but it wont work. I tried the google search but still i cant figure out why it wont work. I am replicating the resident evil 5's camera control. I have a character that needed to limit the rotation of child camera's x from 0 to 30 only. How can i do that? Please help me. I'll show my code:

 void Start()
 {
     Screen.lockCursor = true;
     

     ////Get the name of Player and Main Camera tag
     cameraGO = GameObject.FindGameObjectWithTag("MainCamera");
     cameraTargetGO = GameObject.FindGameObjectWithTag("Camera Target");
     localPlayerGO = GameObject.FindGameObjectWithTag("Local Player");
 }

 void LateUpdate()
 {
     xRotation = Input.GetAxis("Mouse X") * Sensitivity;
     yRotation = -Input.GetAxis("Mouse Y") * Sensitivity;
     
     //Rotate the character along y-axis
     localPlayerGO.transform.Rotate(0, xRotation, 0);
     
     //Rotate the Camera along x-axis
     cameraTargetGO.transform.Rotate(yRotation, 0, 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

Answer by whydoidoit · Oct 10, 2012 at 04:27 PM

Try this:

   cameraTargetGO.transform.Rotate(yRotation, 0, 0);
   var current = cameraTargetGO.transform.rotation.eulerAngles;
   cameraTargetGO.transform.rotation = Quaternion.Euler(Mathf.Clamp(current.x, 0, 30), current.y, current.z);
Comment
Add comment · Show 5 · 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 sephiroth111 · Oct 11, 2012 at 12:51 AM 0
Share

when the camera riches 30 value of clamp its ok but when it rich the 0 value it reset to 30. i dont know why

avatar image whydoidoit · Oct 11, 2012 at 09:57 AM 0
Share

It's because < 0 is actually an angle that goes around the 360 degree mark. Hang on let me fix that

avatar image whydoidoit · Oct 11, 2012 at 09:57 AM 0
Share
     cameraTargetGO.transform.rotation = Quaternion.Euler($$anonymous$$athf.Clamp(current.x > 180 ? current.x - 360 : current.x, 0, 30), current.y, current.z);
avatar image sephiroth111 · Oct 11, 2012 at 02:54 PM 0
Share

what do you call about the code with question mark? it doesnt work but thank you for trying to help me.

avatar image kmeboe · Oct 11, 2012 at 05:02 PM 0
Share

That's called the ternary operator (http://en.wikipedia.org/wiki/%3F:). It's really not working for you? You can always replace it with conditional code, like this:

         cameraTargetGO.transform.Rotate(yRotation, 0, 0);
         var current = cameraTargetGO.transform.rotation.eulerAngles;
         
         float xAngle = current.x;
         if (xAngle > 180)
         {
             xAngle -= 180;
         }
         
         cameraTargetGO.transform.rotation = Quaternion.Euler($$anonymous$$athf.Clamp(xAngle, 0, 30), current.y, current.z);

$$anonymous$$ike's answer is a lot prettier, though. What exactly isn't working?

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Limiting player movement (Partially done) 1 Answer

Potential 4.3.1f1 bug Instantiate c# 2 Answers

Drawing flat arrows 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