• 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 PaxStyle · May 26, 2014 at 04:19 PM · rotationtransformangle

Add max angle rotation

Hey.. I've no idea how add a max angle to this script.. because so the rotation is at 360° i would make this:

alt text

 #pragma strict
 
 function Update () { 
 
 if(Input.GetKey(KeyCode.Z))
 { 
 transform.Rotate (2.5, 0, 0 * Time.deltaTime); 
 }
 
 if(Input.GetKey(KeyCode.X))
 {
 transform.Rotate (-2, 0, 0 * Time.deltaTime);
 }
 
 }

 


thank you in advance

22647-1.png (12.2 kB)
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 Kiwasi · May 27, 2014 at 09:18 AM 0
Share

What is your actual question?

To add a max value use $$anonymous$$athf.Clamp

$$anonymous$$ultiplying Time.DeltaTime by 0 will give you 0 every time. But at the cost of performance.

avatar image HarshadK · May 27, 2014 at 09:24 AM 0
Share

Check answer to this question: Limit local rotation

avatar image PaxStyle · May 27, 2014 at 11:03 AM 0
Share

@Bored$$anonymous$$ormon , i have to add a max and $$anonymous$$ rotation angles..because in the script that i've posted the totation is at 360°.. the picture that i've posted explains well what i'm trying to do..

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by francomarini1982 · Apr 14, 2019 at 05:22 AM

Hi @PaxStyle eulerAngles dont give a full 360° result so let's check this example.

1- Camera Rotation must be (xxx,0,0) in the inspector or eulerAngles Y and Z will change and functions won't work.

2- This only works with 1st and 3rd person cameras. Must be changed for other ones.

3- Camera must only rotate on X-Axis. Y and Z axis need to rotate in a parent to avoid values go crazy.

Now check real eulerAngles results:

alt text

Now let's code functions that converts eulerAngle to full 0°-360° range and sets foward view from 0° to 180° and back to >180° to 360°.

 public float EulerXAxisTo360(Vector3 eulerAngle)
     {
         float tmp = eulerAngle.x;
         if (eulerAngle.x >=0f && eulerAngle.x <=90f)
         {
             if  (Mathf.Round(eulerAngle.y)==0)
             {
                 tmp = eulerAngle.x;
             }else
             {
                 tmp = 180f - eulerAngle.x;
             }
         }else if(eulerAngle.x >= 270f && eulerAngle.x <= 360f)
         {
             if (eulerAngle.y == 180f)
             {
                 tmp = 360 - eulerAngle.x + 180f;
             }
             else
             {
                 tmp = eulerAngle.x;
             }
         }
         tmp += 90;
         
         //AVOID GOING OUTSIDE 0-360 RANGE
         if (tmp < 0) tmp = 360 - tmp;
         if (tmp >= 360) tmp -= 360f;
 
         return tmp;
     }


 

Now let's do it backwards. Note that CurrentY and CurrentZ are always camera's transform.eulerAngles.y and transform.eulerAngles.z :

 public Vector3 Full360toEulerXAxis(float Angle,float CurrentY, float CurrentZ)
     {
         Vector3 tmp = new Vector3(0, 0, 0);
         float eulerXAngle=Angle;
         
         if (Angle>=0 && Angle<90)
         {
             eulerXAngle += 270;
             
         }else if (Angle>=90 && Angle<180)
         {
             eulerXAngle -= 90;
         }else if (Angle>=180 && Angle<270)
         {
             eulerXAngle = 360 - eulerXAngle - 90 ;
         }else if (Angle >= 270 && Angle <= 360 )
         {
             eulerXAngle = 360 - eulerXAngle + 270;
         }
         //AVOID GOING OUTSIDE 0-360 RANGE
         if (eulerXAngle < 0) eulerXAngle = 360 - eulerXAngle;
         if (eulerXAngle >= 360) eulerXAngle -= 360f;
 
         tmp = new Vector3(eulerXAngle, CurrentY, CurrentZ);
         return tmp;
     }


Finally you can clamp angles, looking you graphic lets say that you want to be able to see between 45° and 135° for example:

 private float CameraXAngle;
     public float RotationSpeed =2.5f;
     public float MinAngle = 45f;
     public float MaxAngle = 135f;
 
     void Update()
     {
         
         if (Input.GetKey(KeyCode.Z))
         {
             transform.Rotate(RotationSpeed, 0, 0 * Time.deltaTime);
         }
 
         if (Input.GetKey(KeyCode.X))
         {
             transform.Rotate(-RotationSpeed, 0, 0 * Time.deltaTime);
         }
         CameraXAngle = transform.eulerAngles.x;
         if (EulerXAxisTo360(transform.eulerAngles)< MinAngle)
         {
             transform.eulerAngles = Full360toEulerXAxis(MinAngle, transform.eulerAngles.y, transform.eulerAngles.z);
         }else if (EulerXAxisTo360(transform.eulerAngles)> MaxAngle)
         {
             transform.eulerAngles = Full360toEulerXAxis(MaxAngle, transform.eulerAngles.y, transform.eulerAngles.z);
         }
 
     }

 

You can check all the values with OnGui:

 void OnGUI()
     {
         GUI.Label(new Rect(0, 0, 200, 200), "Full 360°=" + EulerXAxisTo360(transform.eulerAngles).ToString());
         GUI.Label(new Rect(0, 20, 200, 200), "EulerAngles=" + transform.eulerAngles.ToString());
         GUI.Label(new Rect(0, 40, 500, 200), "360°toEuler=" + Full360toEulerXAxis(EulerXAxisTo360(transform.eulerAngles), transform.eulerAngles.y, transform.eulerAngles.z).ToString());
     }


camerarotation.png (43.7 kB)
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
avatar image
0

Answer by Berenger · May 27, 2014 at 09:13 AM

Do something along those lines :

 if( transform.eulerAngles.x < min )
     transform.rotation = Quaternion.Euler(min, 0, 0);
 else if( transform.eulerAngles.x > max )
     transform.rotation = Quaternion.Euler(max, 0, 0);

You'll probably have to do something a bit more clever if the angle goes around 0 or 360.

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 Berenger · May 27, 2014 at 09:14 AM 0
Share

By the way, the delta time is multiplying 0, I don't think that's what you had in $$anonymous$$d.

avatar image PaxStyle · May 27, 2014 at 10:58 AM 0
Share

ehm i declared the var $$anonymous$$ and max and pasted this piece of code to my script but unity editor gives me an error... :(

avatar image
0

Answer by francomarini1982 · Apr 14, 2019 at 05:23 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CAMTEST : MonoBehaviour
 {
     public float RotationSpeed = 10f;
     public float MinAngle = 10f; //CAN'T GO BELOW ROTATION SPEED
     public float MaxAngle = 180f;
     void Update()
     {
         
 
         if (Input.GetKey(KeyCode.Z))
         {
             transform.Rotate(RotationSpeed, 0,  0);
         }
 
         if (Input.GetKey(KeyCode.X))
         {
             transform.Rotate(-RotationSpeed, 0, 0);
         }
 
         if (MinAngle < RotationSpeed) MinAngle = RotationSpeed;
 
         if (EulerXAxisTo360(transform.eulerAngles)<= MinAngle)
         {
             transform.eulerAngles = Full360toEulerXAxis(MinAngle, transform.eulerAngles.y, transform.eulerAngles.z);
         }else if (EulerXAxisTo360(transform.eulerAngles)>= MaxAngle)
         {
             transform.eulerAngles = Full360toEulerXAxis(MaxAngle, transform.eulerAngles.y, transform.eulerAngles.z);
         }
 
     }
     public Vector3 Full360toEulerXAxis(float Angle,float CurrentY, float CurrentZ)
     {
         Vector3 tmp = new Vector3(0, 0, 0);
         float eulerXAngle=Angle;
         
         if (Angle>=0 && Angle<90)
         {
             eulerXAngle += 270;
             
         }else if (Angle>=90 && Angle<180)
         {
             eulerXAngle -= 90;
         }else if (Angle>=180 && Angle<270)
         {
             eulerXAngle = 360 - eulerXAngle - 90 ;
         }else if (Angle >= 270 && Angle <= 360 )
         {
             eulerXAngle = 360 - eulerXAngle + 270;
         }
         //AVOID GOING OUTSIDE 0-360 RANGE
         if (eulerXAngle < 0) eulerXAngle = 360 - eulerXAngle;
         if (eulerXAngle >= 360) eulerXAngle -= 360f;
 
         tmp = new Vector3(eulerXAngle, CurrentY, CurrentZ);
         return tmp;
     }
     public float EulerXAxisTo360(Vector3 eulerAngle)
     {
         float tmp = eulerAngle.x;
         if (eulerAngle.x >=0f && eulerAngle.x <=90f)
         {
             if  (Mathf.Round(eulerAngle.y)==0)
             {
                 tmp = eulerAngle.x;
             }else
             {
                 tmp = 180f - eulerAngle.x;
             }
         }else if(eulerAngle.x >= 270f && eulerAngle.x <= 360f)
         {
             if (eulerAngle.y == 180f)
             {
                 tmp = 360 - eulerAngle.x + 180f;
             }
             else
             {
                 tmp = eulerAngle.x;
             }
         }
         tmp += 90;
         
         //AVOID GOING OUTSIDE 0-360 RANGE
         if (tmp < 0) tmp = 360 - tmp;
         if (tmp >= 360) tmp -= 360f;
 
         return tmp;
 
         
     }
     void OnGUI()
     {
         GUI.Label(new Rect(0, 0, 200, 200), "Full 360°=" + EulerXAxisTo360(transform.eulerAngles).ToString());
         GUI.Label(new Rect(0, 20, 200, 200), "EulerAngles=" + transform.eulerAngles.ToString());
         GUI.Label(new Rect(0, 40, 500, 200), "360°toEuler=" + Full360toEulerXAxis(EulerXAxisTo360(transform.eulerAngles), transform.eulerAngles.y, transform.eulerAngles.z).ToString());
     }
 
 }
 
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.

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

23 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

Related Questions

How can I multiply the rotation of a mirrored object? 1 Answer

Problem with Quaternion.Euler() when rotating object 3 Answers

Limiting a GameObject Rotation 1 Answer

Unity - Decrease/Increase the "Z axis" angle axis on Update() 0 Answers

Natural rotation of sun and moon with 2 axis 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