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 /
This question was closed Apr 27, 2016 at 11:46 AM by AGC for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by AGC · Mar 31, 2015 at 03:16 PM · rotationrotatelimithorizontal

How limit the rotation of an object Or if there is other way to rotate

Hello Every One.

How can i limit the rotation of an object by the Axis of Horizontal (ont Y axis), i know there is method called Mathf.clamp but i don't know how to use it for below code.

If there is another why to Rotate an object instead of the code that i am using below that will be helpful too.

  transform.Rotate(0, Input.GetAxis("Horizontal")*Time.deltaTime*turnSpeed, 0);


thanks to those who help!!!

Note: i Included Time.deltaTime*turnSpeed on the code to so that nobody get confuse

Comment
Add comment · Show 3
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 TRG96 · Mar 31, 2015 at 03:41 PM 0
Share

can you elaborate on the question. I dont understand what you are trying to achieve. Are you trying to limit how much the object can rotate on the Y axis? You could use an if statement, check the rotation of the object and if the rotation is below a certain value then rotate it otherwise dont rotate it.

avatar image Jonesy19 · Mar 31, 2015 at 03:49 PM 0
Share

I'm not sure exactly what you are asking, but your code will detect your "Horizontal Input" (maybe a joystick or mouse), and rotate the gameobject about the Y axis based on the magnitude of your joystick/mouse movement.

Odds are you will want to multiply that by Time.deltatime to normalize the rotation speed.

And like TRG96 said, you can use an if statement to limit the total rotation of the object.

avatar image AGC · Mar 31, 2015 at 10:04 PM 0
Share

@TRG96 Thanks for the quick replay.

Yes i want to limit the rotation of object on the Y axis. I don't know how to use the if statement on that code should i use if (input.GetAxis("Horizontal")) or if you can give an example of the if statement how can i use. i was trying to make reference to transform.Rotate but it was giving error so that's why i am lost about if statement i don't know how to limit that in if statement.

and thanks Jonesy19 and TRG69 again for your quick replies

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by Side.MZG · Mar 31, 2015 at 10:18 PM

The simplest option would be:

 public float rotationSpeed = 5.0f;
     public float minimumValue = -30f;
     public float maximumValue = 30f;
 
     void Update(){
         transform.Rotate(0, Input.GetAxis("Horizontal")*rotationSpeed*Time.deltaTime, 0);
         //The function is Mathf.Clamp(value, min, max)
         transform.eulerAngles = new Vector3(transform.eulerAngles.x, Mathf.Clamp (transform.eulerAngles.y, minimumValue, maximumValue), transform.eulerAngles.z);
     }

However, it has a problem. Unity automatically converts negative angles to their positive counterpart. For example, -10 becomes 350 (360-10). The problem is, that as soon as that happens our code realizes 350 is bigger than 30, so it clamps it. The solution to this is the following:

 public float rotationSpeed = 5.0f;
 public float minimumValue = -30f;
 public float maximumValue = 30f;
 
 void Update(){
     transform.Rotate(0, Input.GetAxis("Horizontal")*rotationSpeed*Time.deltaTime, 0);
     //The function is Mathf.Clamp(value, min, max)
     if(transform.eulerAngles.y < 180){
         transform.eulerAngles = new Vector3(transform.eulerAngles.x, Mathf.Clamp (transform.eulerAngles.y, 0, maximumValue), transform.eulerAngles.z);
     }
     else{
         transform.eulerAngles = new Vector3(transform.eulerAngles.x, Mathf.Clamp (transform.eulerAngles.y-360, minimumValue, 0), transform.eulerAngles.z);
     }
 }

Here we are checking the angle to see from which 'side' of our imaginary circle to tackle the limitation. For all angles over 180 degrees, we assume the original angle was negative and try to recover it. (-10 is converted to 350, and we recover it by doing 350-360)

Comment
Add comment · Show 8 · 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 AGC · Mar 31, 2015 at 10:48 PM 0
Share

Hi Side.$$anonymous$$ZG

thanks for reply

i did use the time.delta time but still i cannot limit my rotation on the Y axis

for example i don't want to turn more then 30 and -30 even if i keep press the button but i don't go over those numbers

avatar image AGC · Mar 31, 2015 at 11:04 PM 0
Share

it still keep turning it doesn't stop rotation on 30 and -30

avatar image AGC · Mar 31, 2015 at 11:10 PM 0
Share

even i did try with FixedUpdate the code is not applying the limit to object rotation

avatar image Side.MZG · Mar 31, 2015 at 11:13 PM 0
Share

I just found out why. We're not clamping the rotation, we're limiting the rotation speed. I'm testing a new version of the code and will place it in a moment

avatar image Side.MZG · Mar 31, 2015 at 11:23 PM 0
Share

There, I've tested this and it works for me. I'm sorry I confused you for so long.

Show more comments

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Limit Rotations with Min and Max on Mobile 1 Answer

Limiting RotateTowards on two axes 1 Answer

Limiting rotation of object, specifically using scroll wheel 2 Answers

Limit GUI Rotation? 2 Answers

Raycasting, rotating a ray 360 degrees 2 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