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 uberokeer · Mar 12, 2013 at 06:21 PM · rotationaxiseulery

how do i Limit camera rotation on Y-Axis?

How can I limit the rotation on My y-axis for My camera? My movement code is:

 var rotateSpeed : float = 3.0;
 function Update () {
 transform.localEulerAngles.x -= Input.GetAxis("Mouse Y") * rotateSpeed;
 }

Thanks in advance!

Comment
Add comment · Show 2
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 hoy_smallfry · Mar 12, 2013 at 06:46 PM 1
Share

complete limitation or partial?

avatar image uberokeer · Mar 13, 2013 at 07:48 AM 0
Share

Complete. Besides the update function it's within, This script is just for my camera to rotate vertically with $$anonymous$$ouse Y.

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by robertbu · Mar 12, 2013 at 06:45 PM

First, a quote from the reference on [localEulerAngles][1]:

Only use this variable to read and set the angles to absolute values. Don't increment them, as it will fail when the angle exceeds 360 degrees.

Typically the way I write code to deal with eulerAngles and localEulerAngles is to maintain my own Vector3 and assign but never read from eulerAngles. For most rotations, there are more than one euler angle representation, so you cannot depend on the values.

 var v3Rotate = Vector3.zero;
 var min = -50;
 var max =  50;
 var rotateSpeed : float = 45.0;
 
 function Start() {
     transform.localEulerAngles = v3Rotate;
 }
 
 function Update () {
     v3Rotate.x += Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime;
     v3Rotate.x = Mathf.Clamp(v3Rotate.x, min, max);
     transform.localEulerAngles = v3Rotate;
 }


Comment
Add comment · Show 3 · 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 robertbu · Mar 12, 2013 at 06:53 PM 0
Share

Actually now that I reread your question, you want to limit the Y axis rotation, but your code only rotates around the X axis. Can you clarify your question?

avatar image uberokeer · Mar 13, 2013 at 07:46 AM 0
Share

Yes i know, I only put this script on my camera. I have the character movement script on my character. I want my character to turn horizontaly. And my camera moves vertically. I just copied the code off, of another answer page. But it seems to work fine. I will go ahead and try this code and tweak it to see if it works. Thanks!

avatar image shamalic uberokeer · Sep 14, 2015 at 08:22 PM 0
Share

Hey, I've got the same problem! Which code did you use? Could you share it?

avatar image
0

Answer by pset · Jul 25, 2017 at 02:30 PM

You can use Quaternion.RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta) function to restrict rotation:

 Quaternion ClampRotationYByLookDirection(Quaternion curentRotation)
 {
     Vector3 lookDirectionY = Vector3.ProjectOnPlane(lookDirection, Vector3.up).normalized;
     Quaternion lookRotation = Quaternion.LookRotation(lookDirectionY);
     Quaternion towardsRotation = Quaternion.RotateTowards(lookRotation, curentRotation, lookDeltaY);
     return towardsRotation;
 }
   
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

13 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

Related Questions

Rotation like in editor 2 Answers

Rotation in 8 way direction 2 Answers

Rotate and Move towards Mouse Point 0 Answers

Lock the Camera's Y Rotation Axis? 0 Answers

Need help in setting a game-object's rotation to euler-degrees. 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