Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Bryaneasy · Mar 22, 2017 at 06:17 PM · c#rotation axis

How to limit the rotation of a RigidBody ?

Hi there,

I have some problems with limitting the X-axis and the Z-axis.

I have a tray with a maze on it, on my scene, and i would like to limit the rotation of the tray.

alt text

Basically, i receive X and Z values from an external device, a RaspBerry actually, communicating with a TCP Server, and updating the rotation of the tray every seconds. My problem here is just the rotation part, wich i want to limit from -30 to 30 for example.

Here's the script attached to the tray:

 public class ControlTray : MonoBehaviour 
 {
     public float speedRot;
 
     private float x;
     private float z;
     private Rigidbody rb; 
 
     void Start() 
     {
         rb = GetComponent<Rigidbody> (); 
         ServerTCP.Level = 0; 
     }
 
     void FixedUpdate() 
     {
         x = ServerTCP.x * speedRot * Time.deltaTime;
         z = ServerTCP.z * speedRot * Time.deltaTime;
 
         Vector3 Moves= new Vector3(x,0.0f,z); 
 
         rb.transform.Rotate (Moves); 
     }
 }

I saw other topic trating the same subject, but none of them actually helped me.

I mean, i saw that Mathf.Clamp seems to be the option i need, but i really didn't understand how to use it.

I'm really looking forwards your answers,

Thanks in advance.

Bryan.

capture.png (101.2 kB)
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
2

Answer by Dave-Carlile · Mar 22, 2017 at 06:19 PM

The Mathf.Clamp documentation is fairly clear and provides examples. Basically the idea is that you pass the value you want to clamp along with the minimum and maximum value. The function returns either the value you passed in if it was within the min and max, or returns the minimum value if the passed value is less than min, or returns the maximum value if the passed value is greater than max.

Here's some pseudocode to show how the function works

 float value = 10.0f;
 float minValue = -30.0f;
 float maxValue = 30.0f;

 if (value < minValue)
   return minValue;
 else if (value > maxValue)
   return maxValue;
 else
   return value;


Using part of your example code to illustrate:

 x = Mathf.Clamp(ServerTCP.x * speedRot * Time.deltaTime, -30.0f, 30.0f);



Comment
Add comment · Show 2 · 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 Bryaneasy · Mar 23, 2017 at 08:20 AM 0
Share

Thank you @Dave-Carlile for your answer.

Your example is pretty good and makes me understand the idea of how it's working.

However, after trying this method with my code, i didn't get any error, but still, my tray continue to rotate after he passed the max, or $$anonymous$$ value, it didn't stop.

avatar image Dave-Carlile · Mar 23, 2017 at 12:25 PM 0
Share

Transform.Rotate adds the rotation you pass in to the object's current rotation. If you want to set a specific rotation you use the Transform.rotation property.

Based on your sample code (and the sample I provided) you're clamping the inco$$anonymous$$g rotation value, then adding that to the object's current rotation. Ins$$anonymous$$d you'll need to clamp the final rotation.

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

7 People are following this question.

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

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

How to get an x, y direction/vector from a z rotation c# 2D 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