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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by TifaOng · Nov 20, 2013 at 03:00 AM · c#rotate objectcusm rotation

Rotate a plane at x axis while fixing its y and z initial rotation value

Hi,

I have a plane that is already fixed at rotation (270, 0, 0) in Unity scene. What I wanna do now is to rotate the the plane around x axis, while mainting y and z at 0. When rotating, the direction to face is Vector3 moveDirection (for example).

Secondly, how to change the value of x or y or z rotation value and fix them when necessary?

For example,

 If moveDirection = right,
 Rotation (x, -630, -1530); //x is a changing value based on moveDirection
 if moveDirection = left,
 Rotation (x, -1170, 990);

For your information, I get the values above by rotating my plane.

I searched around ways to do it but all of them do not have fixing a rotation value while rotating. Please help.

Thanks in advance.

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 TifaOng · Nov 20, 2013 at 09:02 AM 0
Share

Hi $$anonymous$$oor,

What do you wanna see in my screen?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Nov 21, 2013 at 02:13 AM

This is a good case for eulerAngles: save the initial eulerAngles in a member variable and "rotate" mathematically the angles, then assign the variable to eulerAngles:

 public var rotationSpeed: float = 60; // 60 degrees per second
 
 private var euler: Vector3;
 
 function Start(){
   // get the initial eulerAngles:
   euler = transform.eulerAngles;
 }
 
 function Update(){
   // rotate mathematically about X:
   euler.x = (euler.x + rotationSpeed * Time.deltaTime) % 360;
   // update object rotation:
   transform.eulerAngles = euler;
 }

You can modify the other angles at any time - but do it in the euler variable, not directly in transform.eulerAngles: any change in the euler variable will be transferred to the object inside Update.

Comment
Add comment · Show 4 · 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 TifaOng · Nov 21, 2013 at 05:44 AM 0
Share

Hi aldonaletto, Thanks! What about y and z?

avatar image aldonaletto · Nov 22, 2013 at 12:41 AM 0
Share

What exactly do you want to do? Rotate the plane to face the camera? Or rotate the plane with the keys? Or what?

avatar image TifaOng · Nov 22, 2013 at 09:09 AM 0
Share

$$anonymous$$oveDirection is deter$$anonymous$$ed by dragging. I want to rotate the plane to face the endPosition of the drag. However, when I tried to rotate the plane manually in Unity editor, when rotate to left, x varies according to the rotation, y and z changes to another constant (x, -90, 90). When rotate to right, x varies and y and z changes to another constant (x, 90, -90). This is what unity shows.

avatar image aldonaletto · Nov 22, 2013 at 11:23 PM 0
Share

Don't $$anonymous$$d about what appears in the Inspector's Transform/Rotation field: it's actually transform.eulerAngles, a completely "virtual" property that's calculated on the fly based on transform.rotation. Since many different XYZ combinations result in the same rotation, when converting a rotation to Euler angles Unity chooses one of the possible combinations - frequently an unexpected one. If the plane is rotating as expected, forget about the Rotation field.

avatar image
0

Answer by tWayfarer · Nov 20, 2013 at 04:41 AM

If you want to only rotate a single axis, you can do something like:

 tempRotation = currentRotation;
 
 tempRotation.x += rotateSpeed;
 
 currentRotation = tempRotation;

This way, the y or z axis won't change, but x will.

Comment
Add comment · Show 5 · 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 TifaOng · Nov 20, 2013 at 04:58 AM 0
Share

Thanks for the info! But right now, depending on my moveDirection, sometimes y and z will change. Like what I have written in my question. If moveDirection is right, the y and z change and remains a constant until moveDirection is left.

avatar image tWayfarer · Nov 20, 2013 at 06:54 AM 0
Share

What I've written won't affect y or z, even if you change them elsewhere. What it does is get the current rotation, then just change the x, and give it back. Y and z are unaffected by it completely. Then, afterwards (or even before) you can change the y and z as you need to.

avatar image TifaOng · Nov 20, 2013 at 09:07 AM 0
Share

How to change the y and z as I need to? currentRotation will be used in which function?

avatar image tWayfarer · Nov 20, 2013 at 09:44 AM 0
Share

This is more pseudocode than actual code. You'll want something like: Quaternion tempRotation = transform.rotation;

 tempRotation.x += rotateSpeed;
 if(moveDirection == right)
 {
     tempRotation.y = -630;
     tempRotation.z = -1530;
 }
 if(moveDirection == left)
 {
     tempRotation.y = -1170;
     tempRotation.z = -990;
 }
 
 transform.rotation = tempRotation;

It might not work if you copy/paste, but should be close. Unity always recommends NOT just changing x, y, z variables when rotating, because they have functions for it, but I don't know what they are because I don't work with rotation much. It's your best bet to combine this idea with some research into those functions.

avatar image TifaOng · Nov 21, 2013 at 02:01 AM 0
Share

Hi, I understand now. Why tempRotation.x += rotateSpeed? I am changing the x based on the moveDirection, not rotateSpeed.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Face Direction Movement using CharacterController 1 Answer

How do I rotate an object to the cursor?Where is the mistake in the code? 1 Answer

Question on Forcing a Specific Rotation 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