if (myTransform.rotation.eulerAngles.x < 300.0f)
{
myTransform.rotation.eulerAngles.x = 300.1f;
}
As you can figure out the above code doesn’t work, it throws the following error in the console:
“error CS1612: Cannot modify a value type return value of `UnityEngine.Quaternion.eulerAngles’. Consider storing the value in a temporary variable”
What value is this error referring to? and how to do so? I have been trying to figure this out for a while now and clearly I am not getting it.
Basically, when using vectors and quaterions, you can’t modify the values (x,y,z,w) directly. You have to make a new vector or quaternion to replace the old one.
I’m pretty sure it’s because they are structs instead of Classes, but that may not be the case. If anyone has details on that I’d like to know.
You can use the code Daniel showed you, it should work
It throws this error still: “error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.rotation’. Consider storing the value in a temporary variable”
You can either take it another step back and create a temporary var for the rotation as well, or you can access the eulerAngles from the Transform itself.
Thanks again. I tried this already and it doesn’t give me an error but it doesn’t work as I expect, it just make the “myTransform” rotate like crazy constantly. Could you tell me what is the difference between “myTransform.rotation.eulerAngles” and “myTransform.eulerAngles”? I can’t understand it so far.
So now you’ve encountered the second issue, which stems from ignoring this advice, which should frankly be bolded on the front page of the Quaternion.eulerAngles documentation:
Never change only one component of eulerAngles without also setting the other two.
Try this: Create two empty GameObjects. In the inspector, set the rotation of the first to (90,0,0) and the second to (90,90,90). Flip back and forth between them. Notice how they have the same rotation. The quaternion class itself doesn’t know the difference between these two things, and when converting from quaternion to Euler, both of those are perfectly acceptable results of the conversion algorithm. (So are (450,0,0), (810,0,0), (810,450,0), and infinite other combinations, though the algorithm has obvious reasons to reject those variations; not so for 90,0,0 vs 90,90,90) The inspector retains the same XYZ values only because it cheats and saves those values before it converts them to a Quaternion in the first place.
So now here’s the key, and the near-certain source of your crazy rotation. In those GameObjects, change both X values from 90 to 45 - this is more or less equivalent to what you’re doing in your code. Now flip back and forth between them again. The rotations are completely different!
I honestly avoid using Euler angles in script entirely. Instead, using Quaternion.AngleAxis and the * multiplier. This will give you a lot more control over the actual rotation than you’ll ever have with Euler angles.
this code effectively control the rotation of the camera by the input of the player… The problem is:
I am trying to accomplish is a clamping on the up and down rotation so the camera doesn’t go beyond 80 and -80 degrees in the vertical rotation. The problem is that I can’t find a way to do it! Any help would be awesome.
Needing to clamp rotation brings me to my second piece of advice. It’s not nearly as hard and fast as the first one, so I’m only going to italicize it, not bold it:
Don’t rely on transform.rotation to store your rotation data; instead, think of transform.rotation as a means of output, while your real rotation data is handled directly by your script.
And its corollary:
Completely overwrite transform.rotation each frame.
There are naturally exceptions to this one, but I find this to be a good guide especially for anything that the player controls. In this case, you’d probably store a horizontal rotation angle (from 0 to 360, and it loops around), and a vertical rotation angle (from -45 to 45, and it’s clamped). Your input axes modify these numbers, and only after they’re done, should you set your transform’s rotation, using these two numbers within two Quaternion.AngleAxis statements:
Excellent! Thank you very much, I used the second solution and it worked like a charm.
I would love to understand Pi, but I have never been able to. I have tried so many times because I know how important is to understand it but I just can’t. Perhaps I never had a good teacher to explained to me in a way that I can assimilate it or perhaps is me, who knows.
Anyways thank to all of you that help me figure out this and to the advices as well. Very much appreciate it.
Perhaps this explanation will help. (And I highly recommened the other articles at that site, too.)
But for Unity purposes, you rarely need to use Pi. Let me take a stab at explaining it in practical terms.
When you need to measure or describe an angle, there are two standard ways to do it. One is to divide the total circle up into 360 little slices called “degrees,” and then count how many degrees there are in your angle. The other is to say: if you took a string as long as the radius of the circle, how many times would that string lay around the outside of the circle before it gets to your angle? And we call that radians.
It’s easy to see that there are a lot more degrees in a circle than radians. A degree is 1/360th of a circle; a radian is, well, the length of the radius. If you lay that radius string all the way around the circle, it’ll go around about 6 times, plus a little more. This number is sometimes called tau, and it’s actually about 6.2831853072. That’s how many times you can lay a radius-length string all the way around a circle.
So where does pi come into it? Well, pi is defined differently: it’s how many times you can lay a string the length of the diameter around a circle. The diameter is twice as big as the radius, so obviously, pi is half as big as tau. A diameter string will go around the circle about 3 times (more precisely, about 3.1415926536 times). Or, to look at the exact same thing another way, a radius string will go halfway around the circle pi (about 3) times.
All of which means what, to you? Well, it means whenever you’re measuring angles in radians, you just remember that Mathf.Pi represents halfway around the circle, or the same angle we call “180°” when using degrees. Mathf.Pi/2 is a quarter circle, or the old 90 degrees.
But since most functions in Unity are defined in degrees rather than radians, you rarely need to know even that. EulerAngles, rotation parameters, etc. all work in degrees. Degrees are no better or worse than radians; they’re just two different ways of measuring how far around a circle you’re talking about.
About the only place radians come up in Unity is in the trig functions (Mathf.Sin, etc.), because for those to work in radians is very very standard in all programming languages, and there’d be hell to pay if they tried to change it. And you can probably get by a long time without needing the trig functions.
@HoeStrout
Very nice of you to take time to explain in a very clear way those concepts. Thanks so much for the explanation. Very helpful. I will check the link.