• 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 Not_Sure · Sep 14, 2014 at 08:38 PM · rotate

How to set the z rotation and not simply rotate?

I can't seem to figure this out, I want my character's head to tilt as he spins but rather than tilt it keeps turning and goes all "Exorcist".

Here's what I've got:

 var bravelyHead : GameObject;
        /* bunch of cut out code */
         if (play == true){
             if (dead == false){
                 bravelyHead.transform.rotate (0,0, spin);
             }
                 }

The spin variable is has a max of 3 and a minimal of -3. It never goes past them.

I also tried:

 bravelyHead.Transform.Rotation (0,0, spin);

But the moment I apply spin it crashes and I get an error message that says: "NullReferenceException: Object reference not set to an instance of an object."

What am I goofing up?

Comment
Add comment · Show 4
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 Not_Sure · Sep 14, 2014 at 08:01 PM 0
Share

EDIT: I also tried this.

 bravelyHead.transform.rotation = Quaternion.Euler(0, 0, spin * 10);

And it tilted, but now it doesn't keep the z rotation from its parent.

avatar image Scribe · Sep 14, 2014 at 08:41 PM 0
Share

do that with localRotation ins$$anonymous$$d of rotation ?

avatar image Not_Sure · Sep 14, 2014 at 11:21 PM 0
Share

Shouldn't this make the z rotation 30?

 bravelyHead.transform.rotation.z = 10;

But when I hit play it points straight down and not 10 degrees to the right relative to the character and the z value is all over the map.

avatar image rutter · Sep 14, 2014 at 11:25 PM 0
Share

transform.rotation isn't expressed in Euler angles, like you see in the inspector.

Unity stores rotation as a quaternion, internally. Quaternions do have w, x, y and z components, but the behavior of those components is not intuitive so they should almost never be manipulated directly.

Ins$$anonymous$$d, Unity provides helper functions for converting to/from Euler angles, which you'll find much easier to use.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rutter · Sep 14, 2014 at 08:49 PM

If you're trying to manipulate the transform's Euler rotation, it helps to set all three axes. If you're trying to modify only one axis, it helps to keep the other two.

Something like this:

 var euler = bravelyHead.transform.eulerAngles;
 euler.z = spin * 10;
 bravelyHead.transform.eulerAngles = euler;
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 Not_Sure · Sep 14, 2014 at 10:56 PM 0
Share

That still breaks the parent though.

The angle applied to the world position and not relative to the character.

So when my character goes upside down his head is pointing straight up and doing more Exorcist'ing.

Does that make sense?

avatar image rutter · Sep 14, 2014 at 11:00 PM 0
Share

Okay, use localEulerAngles then. Switching between world and local space is an important skill to practice.

avatar image Not_Sure · Sep 14, 2014 at 11:58 PM 0
Share

GOT IT!

 bravelyHead.transform.localEulerAngles.z = spin * 10;

$$anonymous$$an, I guess I didn't understant Euler as well as I thought...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Camera rotation around player while following. 6 Answers

Is it possible to rotate uv coordinates in a script? 1 Answer

Rotate An Object Toward The Direction The Joystick Is Pressed 3 Answers

How can I stop my character from turning on its own after bumping a wall? 1 Answer

i would like to rotate an object from an other objects script 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