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 PiscesIscariot · Jan 18, 2014 at 12:58 AM · rotateaxislocky

Lock the y axis (on an object that's not a rigid body) c#

Hi everybody,

So i'm making a game in which you control a plane (a flat square, not a flying plane haha) to make the objects on it move according to the rotation.

You control the x axis with the Up and Down arrows and the z axis with the Left and Right arrows.

Everything works just fine but I noticed that my plane, somehow, moves as well on the y axis the more you make the plane rotate. I really don't know why. I tried to force the y axis to be locked on 0 but it makes the whole plan (every axis) locked on 0.

Here's my code (the part where I lock the y axis is in the end):

         Vector3 tmp;
         
         tmp = transform.eulerAngles;
         tmp.y = 0;
         
         if(Input.GetKey (KeyCode.UpArrow)){ 
                 if (plan.transform.eulerAngles.x<angleMax || plan.transform.eulerAngles.x>180+angleMax){
                     plan.transform.Rotate(rotation_x);
                 }
         }
         if(Input.GetKey (KeyCode.DownArrow)){
                 if (plan.transform.eulerAngles.x>360-angleMax || plan.transform.eulerAngles.x<180-angleMax){
                     plan.transform.Rotate(-rotation_x);
                 }
         }
 
 
         if(Input.GetKey (KeyCode.LeftArrow)){
                 if (plan.transform.eulerAngles.z<angleMax || plan.transform.eulerAngles.z>180+angleMax){
                     plan.transform.Rotate(rotation_z);
                 }
         }
         if(Input.GetKey (KeyCode.RightArrow)){
                 if (plan.transform.eulerAngles.z>360-angleMax || plan.transform.eulerAngles.z<180-angleMax){
                     plan.transform.Rotate(-rotation_z);
                 }
         }
         if(plan.transform.eulerAngles.y != 0){  //this is where I try to lock it
                 plan.transform.eulerAngles = tmp;  
         }  

Thanks ! :)

Comment
Add comment · Show 2
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 getyour411 · Jan 18, 2014 at 02:20 AM 0
Share

Is this in Update() ?

avatar image PiscesIscariot · Jan 18, 2014 at 03:47 AM 0
Share

Sorry, didn't see your comment! Well "Vector 3 tmp;" is not but the rest is in the Update().

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by HappyMoo · Jan 19, 2014 at 05:31 AM

Unity uses Quaternions internally to represent rotations and there are more than one set of EulerAngles that can represents the same Quaternion, so you don't always get the EulerAngles back that you put in.

if you just want to rotate around x and z, keep the current rotation in float variables rotX, rotZ, let your input code only manipulate those variables and at the end of set the Rotation to new EulerAngles.

So you only write EulerAngles and never read from them.

This should fix your problems.

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 PiscesIscariot · Jan 19, 2014 at 08:30 PM 0
Share

Thanks a lot! But I don't think I really got what you mean, could you write an example with code? I'm a bit lost and confused... :/

avatar image HappyMoo · Jan 19, 2014 at 08:56 PM 1
Share
 private float rotX = 0;
 private float rotZ = 0;
 
 ...
 if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.UpArrow)) 
   rotX = $$anonymous$$athf.Clamp(rotX + rotation_x, angle$$anonymous$$in, angle$$anonymous$$ax);
 if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.DownArrow)) 
   rotX = $$anonymous$$athf.Clamp(rotX - rotation_x, angle$$anonymous$$in, angle$$anonymous$$ax);
 if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow)) 
   rotZ = $$anonymous$$athf.Clamp(rotZ + rotation_z, angle$$anonymous$$in, angle$$anonymous$$ax);
 if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow)) 
   rotZ = $$anonymous$$athf.Clamp(rotZ - rotation_z, angle$$anonymous$$in, angle$$anonymous$$ax);
 
 plane.transform = Quaternion.Euler(rotX, 0, rotZ);

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

20 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

Related Questions

How to lock the y axis in camera script? 2 Answers

Lock Z Rotation. Character Controller. 1 Answer

Rotation in 8 way direction 2 Answers

Lock the Camera's Y Rotation Axis? 0 Answers

Rotate Around Local Y Axis 4 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