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 leandroreschke · Feb 12, 2014 at 08:17 PM · camera rotaterotate objectrelative rotationcusm rotation

Problem with Constraint Camera Rotation C#

 using UnityEngine;
 using System.Collections;
 
 public class ConstraintRotation : MonoBehaviour {
     public enum ConstraintAxis{X = 0, Y, Z};
 
     public ConstraintAxis axis;             // Rotation around this axis is constrained
     public float min;                        // Relative value in degrees
     public float max;                        // Relative value in degrees
     private Transform thisTransform;
     private Vector3 rotateAround;
     private Quaternion minQuaternion;
     private Quaternion maxQuaternion;
     private float range;
     
     void Start()
     {
         thisTransform = transform;
         
         // Set the axis that we will rotate around
         switch ( axis )
         {
         case ConstraintAxis.X:
             rotateAround = Vector3.right;
             break;
             
         case ConstraintAxis.Y:
             rotateAround = Vector3.up;
             break;
             
         case ConstraintAxis.Z:
             rotateAround = Vector3.forward;
             break;
         }
         
         // Set the min and max rotations in quaternion space
         var axisRotation = Quaternion.AngleAxis( thisTransform.localRotation.eulerAngles[ axis ], rotateAround );
         minQuaternion = axisRotation * Quaternion( min, rotateAround );
         maxQuaternion = axisRotation * Quaternion.AngleAxis( max, rotateAround );
         range = max - min;
     }
     
     // We use LateUpdate to grab the rotation from the Transform after all Updates from
     // other scripts have occured
     void LateUpdate() 
     {
         // We use quaternions here, so we don't have to adjust for euler angle range [ 0, 360 ]
         var localRotation = thisTransform.localRotation;
         var axisRotation = Quaternion.AngleAxis( localRotation.eulerAngles[ axis ], rotateAround );
         var angleFromMin = Quaternion.Angle( axisRotation, minQuaternion );
         var angleFromMax = Quaternion.Angle( axisRotation, maxQuaternion );
         
         if ( angleFromMin <= range && angleFromMax <= range )
             return; // within range
         else
         {        
             // Let's keep the current rotations around other axes and only
             // correct the axis that has fallen out of range.
             var euler = localRotation.eulerAngles;            
             if ( angleFromMin > angleFromMax )
                 euler[ axis ] = maxQuaternion.eulerAngles[ axis ];
             else
                 euler[ axis ] = minQuaternion.eulerAngles[ axis ];
             
             thisTransform.localEulerAngles = euler;        
         }
     }
 }

this is my code, and the Console is saying some errors

The best overloaded method match for UnityEngine.Vector3.this[int]' has some invalid arguments Argument #1' cannot convert ConstraintRotation.ConstraintAxis' expression to type int'

The best overloaded method match for `UnityEngine.Quaternion.AngleAxis(float, UnityEngine.Vector3)' has some invalid arguments

Can someone help me? It's a script to Constraint X rotation of the main camera.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Feb 12, 2014 at 08:58 PM

Just cast axis to an int everywhere you use it as an index in an array:

 euler[ (int)axis ] = maxQuaternion.eulerAngles[ (int)axis ];

You also have a '.AngleAxis' missing on line 37.

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 PicturesInDark · May 20, 2014 at 05:27 PM

For this to work: public enum ConstraintAxis{X = 0, Y = 1, Z = 2};

The script works ok!

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 leandroreschke · May 22, 2014 at 10:53 PM

Robertbu already resolved it for me, but your answer is valid if I wanted to use y and z, I made this from a js script, this is why my code has problems, forgot to set INT. But thanks!

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

19 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

Related Questions

really annoying rotation problem. 0 Answers

Changing this code to control camera with keys? 0 Answers

Rotating an already rotated object. 1 Answer

How to use Lerp(Slerp) to rotate on the Z Axis. 3 Answers

Rotating Produces Odd Results [Unity 2D] 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