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 Nk.Andrei · Oct 04, 2013 at 10:06 PM · c#rotationaxisclamp

Constraints on rotation axis in c#

How do I create a constraint on a certain rotation axis in c#?.....making an object rotate towards the mouse is easy but I cant seem to create the x and z constrains to keep it from spinning in every direction. ....... if I use transform.rotation.z = 0 I get an error in C#.

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 vexe · Oct 04, 2013 at 10:13 PM 0
Share

rotation is a Quaternion. you can't mess with its individual components like the way you're doing it. you can set the rotation like:

 transform.rotation = Quaternion.Euler(x, y, z); 

this is just like setting the rotation in the inspector

avatar image vexe · Oct 05, 2013 at 08:37 AM 0
Share

What you mean deformed? - You only want the y to rotate right?

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by vexe · Oct 05, 2013 at 08:45 AM

See this amazing tutorial from UnityGems, covers everything you need about rotations and Quaternions including your restrictions/constraints that you're asking about (the "Rotating Around the Y Axis Only" section)

Comment
Add comment · Show 10 · 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 Nk.Andrei · Oct 05, 2013 at 08:51 AM 0
Share

I know but in javascript when I make an object rotate towards the mouse with the transform.LookAt everything works fine....I just add a constraint on the x and z axis like transform.rotation.z = 0 and everything works fine......... But in c# I cannot set constraints on an individual axis like in javascript so I use tranform.EulerAngles = new Vector3(0,transform.eulerAngles.y,0)....the constraints actually works but the gameobject gets deformed by this and I want to know if there any other ways to set constraints.Bu if I use transform.rotation = Quaternion.Euler(0,transform.rotation.y,0) the gameObject dosent spin anymore it just budges a little from left to right.

 using UnityEngine;
 using System.Collections;
 
 public class turretRotation : $$anonymous$$onoBehaviour {
     
     RaycastHit hit;
     Vector3 targetPos;
     
     
     
     
     void Start () 
     {
         
         renderer.material.color = Color.red;
     }
     
     
     void Update () 
     {
          
         if(Physics.Raycast(Camera.main.ScreenPointToRay (Input.mousePosition),out hit,100))
         {
             targetPos = new Vector3 (hit.point.x, 0, hit.point.z);        
         }
         transform.rotation =  Quaternion.LookRotation(targetPos - transform.position);
         
         
         if(transform.eulerAngles.x!=0 || transform.eulerAngles.z!=0)
         {
             //transform.eulerAngles = new Vector3(0,transform.eulerAngles.y,0 );///the constraints work fine but the gameObject gets deformed
             transform.rotation =  Quaternion.Euler(transform.rotation.x, transform.rotation.y , transform.rotation.z );//the constraints work as well bu the gameObject barely budges from left to right
         }
         
     }
 }

I need to know what Im doing wrong.....in javascript is easier but I want to see it in c#

avatar image vexe · Oct 05, 2013 at 09:08 AM 2
Share

Please do it like in the link I gave you above. rotation is a quternion you shouldn't access/mess with its components directly like you did (it has four components, xyzw):

  transform.rotation =  Quaternion.Euler(transform.rotation.x, transform.rotation.y , transform.rotation.z );

Just make a new rotation variable:

 var newRotation = Quaternion.LookRotation(targetPos - transform.position).eulerAngles;

set your constraints:

 newRotation.z = newRotation.x = 0;

and apply your new rotation:

 transform.rotation = Quaternion.Euler(newRotation);

That should work just fine.

avatar image vexe · Oct 05, 2013 at 09:48 AM 0
Share

you still have the problem?

avatar image vexe · Oct 05, 2013 at 11:31 AM 1
Share

Not sure if it would work, but could you try localEulerAngles ins$$anonymous$$d? - the thing is, eulerAngles gets locked at 360 dgs, you can't rotate more than that. Also put a print between setting the rotation and the eulers, print (trans.euler.y); see what's happening.

avatar image ShawnFeatherly · Mar 13, 2015 at 07:27 PM 1
Share

The linked article is great. Not sure how long it'll work, but mirror at: http://web.archive.org/web/20140702084123/http://unitygems.com/quaternions-rotations-part-1-c/

Show more comments

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

16 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Camera global rotation clamping issue 0 Answers

rotate axis of movement 0 Answers

Clamp Quaternions Rotation for camera 1 Answer

Rotating a specific axis towards a object 3 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