Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by useralpha · Aug 25, 2018 at 06:02 PM · rotationaxisrotation axissingle

How set rotation of a single axis

I am making a spaceship game, and in the game the ship can move forward, backwards, right, and left, and it can turn up, down, right and left. The problem is with the two-axis turning: when you turn one way and then the other, you end up in a roll position. Is there a way to set the rotation so as to prevent a roll without resetting all the rotation axis's value? In other words, is there a way to set the x-rotation to, say, 0 from whatever it is without changing y and z?

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 eses · Aug 25, 2018 at 07:15 PM 0
Share

@useralpha - Hi, I'm not sure but are you describing a gimbal lock situation? I'm not quite sure based on the description. You can set rotations by Euler angles but internally they are Quaternions...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sgt_Spike · Aug 25, 2018 at 07:20 PM

Hey there. Not 100% sure it will work but maybe you could try using this:

yourObjectName.transform.Rotate(yourXVariable, yourObjectName.y, yourObjectName.z)

Make a float for the x variable and change 'yourXVariable' with that float. Or you could just set that x value to any number.

Hopefully it works! :D

Comment
Add comment · Show 3 · 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 useralpha · Aug 25, 2018 at 07:55 PM 0
Share

Well, that wasn't quite it, but I played around a bit and now it works rather well. In the end, I put:

in Start():
float levelRotationX = transform.rotation.x;

in FixedUpdate():
transform.Rotate(levelRotationX - transform.rotation.x, 0, 0);

this allows the player to stay point upwards if they want, but the anti-rolling sometimes rotates to the wrong point. The other option was doing:
in Start():
float levelRotationX = transform.rotation.x;
float levelRotationZ = transform.rotation.z;

in FixedUpdate():
transform.Rotate(levelRotationX - transform.rotation.x, 0, levelRotationZ - transform.rotation.z);

but that means that it will also reset the up/down tilt of the ship. not sure what is better :)

avatar image Sgt_Spike useralpha · Aug 25, 2018 at 08:52 PM 0
Share

Eh, well perhaps you could use a boolean or something to stop the other axis moving. Again, not sure if this will make the problem better or worse. Looking at the code you provided it looks like you need to only rotate on the x and z axis' and the y axis is not used at all. If that's correct make a boolean called 'rotateX' or something. Then when your player needs to rotate on the x axis, set it to true, whilst if it's the z axis set it to false. You can make the code sort of like this.

 public static bool rotateX
 
 Start()
 {
     float levelRotationX = transform.rotation.x;
     float levelRotationZ = transform.rotation.z;
 }
 
 FixedUpdate()
 {
     if(rotateX == true)
     {
         transform.Rotate(levelRotationX - transform.rotation.x, 0, transform.rotation.z); 
     } else
     {
         transform.Rotate(transform.rotation.x, 0, levelRotationZ - transform.rotation.z); 
     }
     
 }

Then just change that boolean from the script that makes your player move. Again, not too sure if this will make it any better or not. Have a look into that and see if you can get it to work! Good luck!

avatar image useralpha Sgt_Spike · Aug 25, 2018 at 09:00 PM 0
Share

Yeah, I think that that is what I will do, and I'll make it so the player can change the bool in-game, since alone, neither option covers everything. Either that or the player will manually control rolling, but that would mean 10 buttons to control basic movement alone. :/

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

182 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 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 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 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 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 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 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 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

Get angle of rotation around local axis 1 Answer

Rotating on other axis 1 Answer

Writing a simple script to rotate an object in direction of axis 0 Answers

Rotate Object Axis On Swipe Cam Rotation - Android 1 Answer

Having two objects Y axis parallel rotating around local Z axis. 0 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