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 EriktheFunk · Sep 18, 2012 at 03:22 AM · rotationverticalhorizontal

Limit rotation (Horizontal and vertical input)

Hello, So I need to limit the rotation on an object that is controlled by keyboard to rotate on both the Z and X axis. I would like to be able to limit how much the object rotates to 20 degrees either direction from the 0 point of rotation. So this would mean the object could rotate -20 degrees and +20 degrees on both x and z.

I have been trying like crazy to use the Mathf.Clamp, however I have yet to get it to work.. things just start popping all over the place.. I could really use a hand on a best method for doing this. Thank you so much in advance.

Here is my code:

 function Start () {
 
 }
 var rotateObject: GameObject;
 
 function Update () {
 
 if (Input.GetAxis("Horizontal") > 0) {
 rotateObject.transform.localEulerAngles.z -= 10 * Time.deltaTime; //right
 }
 
 if (Input.GetAxis("Horizontal") < 0) {
 rotateObject.transform.localEulerAngles.z += 10 * Time.deltaTime; //left
 }
 
 if (Input.GetAxis("Vertical") > 0) {
 rotateObject.transform.localEulerAngles.x += 10 * Time.deltaTime; //forward
 }
 
 if (Input.GetAxis("Vertical") < 0) {
 rotateObject.transform.localEulerAngles.x -= 10 * Time.deltaTime; //backward
 }
 
 }
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
1
Best Answer

Answer by hvilela · Sep 18, 2012 at 03:38 AM

Do like this:

 var tmp = rotateObject.transform.localEulerAngles.z - 10 * Time.deltaTime;
 rotateObject.transform.localEulerAngles.z = Mathf.Clamp(tmp, -20, 20);

EDIT:

 var tmp = rotateObject.transform.localEulerAngles.z - 10 * Time.deltaTime;
 if (tmp < 20 || tmp > 340) {
   rotateObject.transform.localEulerAngles.z = tmp;
 }
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 EriktheFunk · Sep 18, 2012 at 01:35 PM 0
Share

Unfortunately this is not working as intended. When I enter this in, and I try to rotate the object (in this example I am trying to rotate the z into the negative) it pops from 0 rotation when I begin, all the way to +20 on the other direction. after that, if I move a little past +20 and press a key to go the opposite direction, it immediately pops to -20,Part of the problem might be that the rotation for this object when played goes 0,+1,+2, etc going left (pos z) then goes 0,359,358,357,etc going the opposite direction on z.

I want the object to smoothly rotate, starting at the 0 point from 0 to positive 20, then from 0 to -20 degrees of the current position, however in this case it seems to mean 340 degrees. With this solution it seems to just snap to both locations, with no smoothing of the movement...

avatar image hvilela · Sep 18, 2012 at 02:42 PM 0
Share

$$anonymous$$y answer was edited. Check if it solve your problem.

avatar image EriktheFunk · Sep 18, 2012 at 03:31 PM 0
Share

Thank you so much, this works perfectly! Still new to scripting, so I really appreciate the help.

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

12 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

Related Questions

get euler rotation from horizontal and vertical axis values 1 Answer

Possible To Add Move Functions To Camera? 2 Answers

Simple controls for a helicopter 2 Answers

rotate the camera in the z axis 1 Answer

limit AddRelativeForce 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