• 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 /
  • Help Room /
avatar image
0
Question by SolunaGD · Jun 02, 2016 at 02:37 AM · c#rotationscripting problem

How to set limits to a player rotating an object

For practice, I'm making a game w$$anonymous$$ch is like one of those maze games where you rotate the maze itself and direct a ball, avoiding holes. I've managed to allow the maze's rotation to be controlled by the player. However, I can't figure out how to stop the maze from rotating past 30 degrees.

Here's my script that controls t$$anonymous$$s:

 using UnityEngine;
 using System.Collections;
 
 public class MazeController : MonoBehaviour {
 
     public GameObject maze;
     // Use t$$anonymous$$s for initialization
     void Start () 
     {
     
     }
 
     void FixedUpdate () 
     {
         float rotateEW = Input.GetAxis("Vertical");
         float rotateNS = Input.GetAxis("Horizontal");
         if (maze.transform.rotation.x < 30.0)
         {
             maze.transform.Rotate(0.0f, 0.0f, rotateNS);
         }
         if (maze.transform.rotation.z < 30.0)
         {
             maze.transform.Rotate(rotateEW, 0.0f, 0.0f);
         }
     }
 }
Comment
Add comment · Show 1
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 appicarts · Jun 02, 2016 at 06:37 AM 0
Share

try this:

  if (maze.transform.rotation.x < 30.0)
  {
          maze.transform.rotation = Quaternion.Euler (new Vector3 (x,y,z)); //replace x,y,z with whatever the limit is        
   }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by toromano · Jun 02, 2016 at 08:02 AM

Do not directly set euler angles. Use quaternions instead. Here is sample script. I hope you fnd is useful:

 public class RotationTest : MonoBehaviour {
 
     float xangle;
     float zangle;
     // Update is called once per frame
     void Update ()
     {
         zangle += Input.GetAxis("Horizontal");
         xangle += Input.GetAxis("Vertical");
         zangle = Mathf.Min(Mathf.Max(zangle, -30.0f),30.0f);
         xangle = Mathf.Min(Mathf.Max(xangle, -30.0f), 30.0f);
         t$$anonymous$$s.transform.rotation = Quaternion.AngleAxis(xangle, Vector3.right) * Quaternion.AngleAxis(-zangle, Vector3.forward);
     }
 }
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 SolunaGD · Jun 03, 2016 at 01:35 AM 0
Share

This worked, but to a certain extent. Unfortunately, the rotation will be locked when it hits either 1 degree or 359 degrees on both axis. I tried replacing the vector3.right and vector3.forward with the proper limits at their respective axis, but to no avail. If you have a solution to this, I would appreciate if you would tell me.

avatar image toromano SolunaGD · Jun 03, 2016 at 11:58 AM 0
Share

Did you try the code as is? Or you did some modifications? Because when i added the script on a cube, i don't see any limits other than +- 30 degrees

avatar image SolunaGD toromano · Jun 03, 2016 at 11:17 PM 0
Share

That's strange, I have it as-is but it cannot be rotated past 0 or 359 degrees

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

173 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Why my code to rotate object doesn't work in normal runtime, but does in debugging? 0 Answers

2D smooth rotation (Quaterion slerp solution?) 0 Answers

my camera is rotating on Z axis when i just set it on X and Y 0 Answers

How to change rotation for a FirstPersonController through script. 0 Answers

Why won't my model rotate up on X axis? 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