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 /
avatar image
0
Question by Itaden · Sep 28, 2011 at 01:10 PM · c#rotationaxis

Restricting rotation to the y axis in C#

I am trying to keep my enemy object from rotating on the z and x axis while patrolling. I was using this set up in unity script but want to change it to C#

 RotateToward(target.position, turnSpeed);
     transform.rotation.x = 0;
     transform.rotation.z = 0;


I have so far tried the following in C# with no success

     Quaternion enmeyRotationX = transform.rotation;
     Quaternion enmeyRotationZ = transform.rotation;

     RotateToward(target.position, turnSpeed);

     enmeyRotationX.x = 0f;

     enmeyRotationZ.z = 0f;

     transform.rotation = enmeyRotationX;

     transform.rotation = enmeyRotationZ;

Searching through the Q&A on these boards I found many solutions but they also appear to be for Jscript and I dont know how to translate it.

Many thanks

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
8
Best Answer

Answer by syclamoth · Sep 28, 2011 at 01:28 PM

I'm afraid Quaternions don't work the way you think they do. If you wanted to restrict a transform's rotation, you could use something like this-

 Vector3 eulerAngles = transform.rotation.eulerAngles;
 eulerAngles = new Vector3(0, eulerAngles.y, 0);
 transform.rotation = Quaternion. Euler(eulerAngles);

Alternatively, you could automatically squash the angle down in the previous step-

 Vector3 correctTarget = new Vector3(target.position.x, transform.position.y, target.position.z);
 transform.LookAt(correctTarget);

and not worry about the exact Quaternions at all! There are very few situations where you would want to manually change the contents of a quaternion. Unless you are very familiar with the (literally complex) mathematics of them, there's no point trying, since the Quaternion class provides plenty of utilities for creating and modifying quaternions in ways which are much easier to understand!

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 volcan8bit · Jan 06, 2016 at 06:44 AM 0
Share

Do you think you could help me with my similar problem? I need to have an object rotate on the x axis all 360 degrees without y and z axis changing their values at all. This is the code I have so far. Thanks in Advance please help I've spent 5 hours researching how to fix this problem.

 public float transition = 0.0f;
 public float CycleSpeed = 1.0f;

 void Start () 
 {

 }
 
 void Update () 
 {
     Vector3 eulerAngles = transform.rotation.eulerAngles;
     eulerAngles = new Vector3(eulerAngles.x, 0, 0);
     transform.rotation = Quaternion. Euler(eulerAngles);
     transform.Rotate(Vector3.right, Time.deltaTime * CycleSpeed, Space.World);
 }

The Current Error I get is the x axis rotates at a constant rate perfectly until it reaches 90 degrees where it freezes.

avatar image LazyElephant volcan8bit · Jan 06, 2016 at 07:27 AM 0
Share

@volcan8bit check out Quaternion.AngleAxis http://docs.unity3d.com/ScriptReference/Quaternion.AngleAxis.html

avatar image
1

Answer by Waz · Sep 28, 2011 at 10:54 PM

syclamoth's answer is correct, but here is a direct translation of the (incorrect) JavaScript:

 RotateToward(target.position, turnSpeed);
 var r = transform.rotation;
 r.x = 0;
 r.z = 0;
 transform.rotation = r;

This will leave you with an incorrect w, just as the JavaScript does, but hopefully it expains what was wrong with your attempts.

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 Itaden · Sep 29, 2011 at 03:02 AM

Thanks guys worked wonders and makes my script look allot cleaner too

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Is there a way to lock my camera's rotation and movement on certain axis? 2 Answers

how to lock the z axis rotation 2 Answers

Get localScale depending on rotation 1 Answer

How to rotate on one axis while keeping the other axes open to be rotated by other scripts? 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