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 SuperBarrio · May 16, 2011 at 04:35 PM · rotationlimitrestrict

Restricting Rotation

Im making a Tilting Labyrinth (Or Super Monkey Ball) style game and have knocked up a simple script to rotate the terrain.

Im hoping somebody will be able to tell me what changes to make to the script so that I can restrict the rotation (in both directions) between two points.

Thanks in advanced for any help.

var rotateSpeed = 3.0;

function Update () { //Rotate around z- axis transform.Rotate(0, 0, Input.GetAxis ("Vertical") * rotateSpeed);

//Rotate around z - axis transform.Rotate(Input.GetAxis ("Horizontal") * rotateSpeed, 0, 0);

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Cameron 3 · May 16, 2011 at 05:09 PM

You could check the euler angles of a rotation and use those for a comparison. It can be tricky, but you can access the euler angles pretty easily:

Debug.Log( "Current Euler Angles: " + transform.eulerAngles );

Put that underneath your 2nd transform.Rotate and you'll get a console log that will tell you the current euler angles. You can use those to write an If statement which you could surround your transform.Rotate's with in your update function.

something like:

function Update ()
{
     if (transform.eulerAngles.z < 90)
     {
         transform.Rotate(0, 0, Input.GetAxis ("Vertical") * rotateSpeed)
     }
}

I don't know if this will exactly work for your situation but it may put you on the right track. Also there are localEulerAngles which might be better depending on your situation. This can be tricky to do right because of wrapping issues from negative degrees to positive degrees and for angles that move past 360.

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 SuperBarrio · May 17, 2011 at 07:23 PM 0
Share

I tried it, and didn't have much luck, the second script you've written seemed to not move the plane atall - but thankyou for your help. I will keep trying!

avatar image
0

Answer by DiebeImDunkeln · Oct 02, 2015 at 08:14 AM

If you only check the eulerAngles than you have the problem that you start at angle 0 and in the negativ direction you get get 365 instead of -1. This makes it really difficult to set the restriction values. So make a helper variable like "vRotation" in my case. This can have a positive and negative value, which can be restricted.

For example you want to restrict vertical axis, in my case it is the x axis:

 float vRotation = 0;
 
 void FixedUpdate()
 {
  float v = Input.GetAxis ("Vertical") * rotateSpeed;
  float angX = transform.eulerAngles.x;
 
  vRotation += v;
 
  if(vRotation > 20)
  {
      vRotation =20;
  }
  if(vRotation < -20)
  {
      vRotation =-20;
  }
 if(vRotation >-20 && vRotation <20)
 {
  transform.Rotate(angX+v,0,0);
 }
 
 }

I wrote this from my memory, so I hope i wrote it correctly.

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

1 Person is following this question.

avatar image

Related Questions

Limiting rotations to box edge 1 Answer

Transform.rotate limits 3 Answers

Camera Limit Rotations Problem 1 Answer

simple plane code how to limit rotation 2 Answers

Limit distance from center 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