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
1
Question by Sudhi · Aug 07, 2012 at 02:29 AM · transforminputrotatekeys

Rotate an object with angle limit

I want to rotate an object with arrow keys. Here is the code I used for the rotation ,

     if(Input.GetKey(KeyCode.UpArrow))
      {
      transform.Rotate(Vector3.right,1,1);
      }
  
  if(Input.GetKey(KeyCode.DownArrow))
  {
  transform.Rotate(Vector3.left,1,1);
  }
  
  if(Input.GetKey(KeyCode.RightArrow))
  {
  transform.Rotate(Vector3.up,1,1);
  }
  
  if(Input.GetKey(KeyCode.LeftArrow))
  {
  transform.Rotate(Vector3.down,1,1);
  }

This works,

But, I want to limit the rotation of this object, which is a disc on top-down view. Example, if I press uparrow, the object should only be able to rotate till 15deg towards Vector3.right. Same with all the other keys.

How do I achieve this?

Thanks in advance.

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

Answer by aldonaletto · Aug 07, 2012 at 03:16 AM

The best way to control rotation precisely in Unity is to "rotate" the angles mathematically and assign them to transform.eulerAngles each Update. To do this, save the initial eulerAngles in a Vector3 variable (initialAngles) at Start, and have another Vector3 variable (curAngles) to show the rotation relative to initialAngles. Increment or decrement curAngles with the controls and clamp them to the limits, then set transform.eulerAngles to initialAngles + curAngles. In order to make the rotation frame rate independent, make the increments/decrements proportional to Time.deltaTime:

var rotSpeed: float = 30; // rotation speed in degrees/second private var initialAngles: Vector3; private var curAngles: Vector3; // rotation relative to initial direction

function Start(){ initialAngles = transform.eulerAngles; curAngles = Vector3.zero; }

function Update(){ if (Input.GetKey("up")){ curAngles.x -= rotSpeed Time.deltaTime; } if (Input.GetKey("down")){ curAngles.x += rotSpeed Time.deltaTime; } if (Input.GetKey("left")){ curAngles.y -= rotSpeed Time.deltaTime; } if (Input.GetKey("right")){ curAngles.y += rotSpeed Time.deltaTime; } // limit the angles: curAngles.x = Mathf.Clamp(curAngles.x, -15, 15); curAngles.y = Mathf.Clamp(curAngles.y, -15, 15); // update the object rotation: transform.eulerAngles = initialAngles + curAngles; }

Comment
Add comment · Show 1 · 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 Sudhi · Oct 15, 2012 at 05:41 PM 0
Share

Thanks, it worked. I wasn't working on this project for a while and completely forgot that I got an answer from you. sorry for the delay. :)

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to prevent Z axis rotation ? 1 Answer

How to prevent Z axis rotation ? 0 Answers

Will not rotate when going different speed? 1 Answer

I want to make responsive homescreen. how to make responsive homescreen UI like this example ? 0 Answers

rotation script in unity 3 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