- Home /
Quaternion to Vector3,Player transform in vector3?
Hi, i am an absolute beginner at unity and was wondering: If you get the rotation of a GameObject by using GameObject.transform.rotation
it comes out as a Quaternion. Is there any way to change that into a Vector3? ,Hi, i'm an absolute beginner at unity, and i was wondering
If you have a GameObject and you get it's rotation using player.transform.rotation
, it comes out as a Quaternion.
Is there anyway to change that into a Vector3?
Answer by JVene · Jul 21, 2018 at 03:09 AM
@TheEndcaster, one must ask what you might expect from a Vector3? There is a conditional answer to your question.
If what you want is the angle of rotation for this Quaternion on the x, y and z axis, you can use the eulerAngles property. It returns a Vector3 of those 3 angles in degrees.
That basically contains what you see in the inspector when it shows the rotation of an object.
Since you are a beginner, let me extend the answer with a few points about concepts your question hints but doesn't ask.
When you say "comes out as a Quaternion", that is because the type of the rotation member of the transform is a Quaternion.
When you "get the rotation", you're reading the property.
Properties are a kind of intelligent version of member variables. The can take specific actions, written in code, when you read or write to them. This is what happens when you use:
Vector3 angles = player.transform.rotation.eulerAngles;
In this code, a Vector3 is assigned a copy of a Vector3 created in the "get" operation of the property "eulerAngles", which at that moment is calculated form the internals of the Quaternion for you. Even though there is no function call operator ( the () that would follow the name of a function when a function is called ), the "get" operation of the property implies a function is being called.
Unity uses Vector3 for several 3 parameter needs, not all of which are actually vectors. A vector implies a direction and a distance. It looks like a point, but the implication is that it is a point relative to the origin. However, when you read the eulerAngles property (and in other contexts), Unity must return an object with 3 float values, so they use the Vector3 - but there is no resemblance to an actual vector in what it returns, and can't be used as a vector in other contexts.
Also, GameObject, spelled with a leading capital, is a type. A type, to C#, specifies the kind of data. It can be used to declare new values (like the Vector3 line I posted above), or it can be used to gain access to static member functions and variables. When you typed "GameObject.transform.rotation", we all know what you mean, but I wanted to point out that this is not a valid statement in C# because there is no static member "transform" in the GameObject class.
In Unity there is a member of several components and classes called gameObject, spelled with a lower case g. This is an instance. It is the formed, working object of the type GameObject, and it does have a transform member.
Your answer
Follow this Question
Related Questions
Perpendicular Vector3 0 Answers
How to use Quaternion to rotate an object in only one axis? 1 Answer
Planetoid Gravity 1 Answer
How is the rotation of a Transform converted into a Vector3 in the inspector ? 2 Answers
Can someone explain this part of the PlayerMoveController.js script in the AngryBots demo? 3 Answers