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 Hamesh81 · Oct 30, 2013 at 02:18 PM · positionvariableaxiseuleranglesstore

How can I store each axis of a transform's rotation as a separate variable?

Quite simply, in the Start function I am trying to store the initial x, y, and z rotation values of a character into 3 separate float variables, NOT 1 Vector3 variable. I tried using transform.position.x or transform.eulerAngles.x to store the x rotation for example but I got the:

Cannot modify a value type return value of `UnityEngine.Transform....'. Consider storing the value in a temporary variable

So how could I do this? I can't use a temporary variable because I will need to check these values in different sections of the code, maybe even across several components. I also need each axis stored separately because I will be making checks against each separately. Any suggestions for this small dilemma?

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 ArkaneX · Oct 30, 2013 at 02:35 PM 0
Share

Even if you plan to check against each axis separately, why do you have to store them as separate variables? You can store initial rotation in Vector3 and then test against x, y and z properties of this vector...

2 Replies

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

Answer by robertbu · Oct 30, 2013 at 02:29 PM

Your error does not match your description. Pulling variables out of rotation will not generate this error, but putting them back in will. For example in C# you cannot do:

 transform.eulerAngles.x = 45;

Plus even if you could, the reference warns against assigning axes independently. It will cause strange rotations. You can do this:

 public class Bug10 : MonoBehaviour {
 
     float x, y, z;
     
     void Start () {
         x = transform.eulerAngles.x;
         y = transform.eulerAngles.y;
         z = transform.eulerangles.z;
         
         Vector3 v3;
         v3.x = x;
         v3.y = y;
         v3.z = z;
         
         transform.eulerAngles = v3;
     }
 }

Note the use of the temporary variable v3 when doing the assignment.

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 aldonaletto · Oct 30, 2013 at 02:48 PM 0
Share

Hey @robertbu: it should be eulerAngles, not rotation !

avatar image Hamesh81 · Oct 30, 2013 at 02:48 PM 0
Share

Aaaah silly me, I wrote the variables in the wrong order! You're right it can be done, sorry for wasting your time. Facepalm

avatar image robertbu · Oct 30, 2013 at 03:16 PM 0
Share

@aldonaletto - thank you!

avatar image Datapax · Nov 06, 2016 at 08:34 PM 0
Share

Hey @robertbu: it should be z = transform.eulerAngles.z not z = transform.eulerangles.z

avatar image
4

Answer by aldonaletto · Oct 30, 2013 at 02:45 PM

Are you using C#? Modifying "value type" elements from a property isn't allowed in C#, but the friendly JS compiler does the job for us. In C# you must save the property to a temporary variable, modify the component in it and assign the variable back to the property. If you want to just modify X in transform.position, for instance, you must do this:

 Vector3 tmp = transform.position; // read property in a temp variable...
 tmp.x = 1.5f; // modify the desired component...
 transform.position = tmp; // and assign the variable back to the property

But be careful when doing this with eulerAngles: you must save the initial value in a Vector3 member variable and never read eulerAngles again ! Do any change you want in the variable only and assign it to eulerAngles for the changes to take effect:

 Vector3 euler0;
 
 void Start(){
   euler0 = transform.eulerAngles; // read initial euler angles only once
 }

When you want to set the X component, for instance, do it as follows:

   euler0.x = 90;
   transform.eulerAngles = euler0;

Never, jamais, under no circumstances modify individual components of eulerAngles - always assign the whole Vector3 to it, like above. eulerAngles is a completely virtual property: Unity internally reads transform.rotation, calculates and returns a combination of XYZ angles that corresponds to the rotation. Since there are several different combinations that result in the same rotation, Unity may (and probably will) return the wrong one.

Comment
Add comment · Show 1 · 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 Hamesh81 · Oct 30, 2013 at 02:59 PM 0
Share

Thanks aldonalleto, this is very helpful information!

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

19 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

Related Questions

Store startposition in a variable 1 Answer

Record Object's Position 2 Answers

Rotating on a plane 2 Answers

how to know if an axis has been full rotated? 1 Answer

How to control an object with a mouse? 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