- Home /
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?
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...
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.
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.
Your answer
Follow this Question
Related Questions
Store startposition in a variable 1 Answer
Record Object's Position 2 Answers
Rotating on a plane 2 Answers