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 santoshadari · Jan 25, 2013 at 08:38 AM · javascriptgame developmentgamedesign

Can anyone help out form this problem please its urgent

I am very new to unity i want to restrict my first person controller to restrict the rotation in x-direction(i.e., horizontal direction). I just workout on a code i works to restrict the rotation in y-axis but i cant restrict it in x-axis direction please help me to solve this problem. My code is

 enum ConstraintAxis
 {
     X,
     Y,
     Z
 }
 
 public var axis : ConstraintAxis;             // Rotation around this axis is constrained
 public var min : float = -20;                        // Relative value in degrees
 public var max : float =  20;                        // Relative value in degrees
 private var thisTransform : Transform;
 private var rotateAround : Vector3;
 private var minQuaternion : Quaternion;
 private var maxQuaternion : Quaternion;
 private var range : float;
 
 function 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;
             
     }
     
     // Set the min and max rotations in quaternion space
     var axisRotation = Quaternion.AngleAxis( thisTransform.localRotation.eulerAngles[ axis ], rotateAround );
     minQuaternion = axisRotation * Quaternion.AngleAxis( 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
 function 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;        
     }
 }
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
0

Answer by cdrandin · Jan 25, 2013 at 09:06 AM

Not to be picky and all, but I think constraint axis should be a class of its own. Like..

 public class ConstraintAxis
 {
     public bool x;
     public bool y;
     public bool z;
 }

Then call it like var freezeRotation : ConstraintAxis , which will look just like the freezerotation in the inspector of a rigidbody. So you can do all your rotations and what not, BUT if one of the members in ConstraintAxis is true, restrict the movement from its default rotation. Where is this default rotation? In the start function save that data and you should be good to go.

So to show code:

 //globals
 var defaultRot : Quaternion;
 ...
 
 public function Start (){
     defaultRot = transform.rotation;
 }
 
 public function LateUpdate (){
 ...
 //You currently have gotten all inputs, movement, rotations have been adjusted, before we send this info to Move(...)
 
 if(ConstraintAxis.x)
    transform.rotation.x = defaultRot.x
 
 if(ConstraintAxis.y)
    transform.rotation.y = defaultRot.y
 
 if(ConstraintAxis.z)
    transform.rotation.z = defaultRot.z
 
 //call Move(...)

Not sure if this will work, but I think it would or at the very least the idea is there.

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 cdrandin · Jan 25, 2013 at 09:13 AM 0
Share

Ok I think I did the rotation write wrong. i.e transform.rotation.x Um, trying to find out how to change the axis rotation right now..

avatar image cdrandin · Jan 25, 2013 at 09:15 AM 0
Share

O$$anonymous$$, you will need Transform.eulerAngles so.. transform.eulerAngles.x, but now I am not sure what type to store the defaultRot so it will fit in nicely with eulerAngles.

Sorry I am all over the place a bit tired, but hopefully you have a general idea now.

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

10 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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Script to teleport player (Beginner here) 0 Answers

How to update scoreline on a HIT? 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