- Home /
How do I do operations with a Vector3 and a Quaternion?
I'm trying to do a basic camera rotation, but it won't let me set a Vector3 to the camera's rotation, saying: Assets/Scripts/Camera Rotate.cs(11,32): error CS0029: Cannot implicitly convert type UnityEngine.Quaternion' to
UnityEngine.Vector3'
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraRotate : MonoBehaviour { public float speed = 20f; void Update() { Vector3 rotation = transform.rotation; if (Input.GetKey ("u")) { rotation.y += speed * Time.deltaTime; } transform.rotation = rotation; } }
Answer by Bunny83 · Mar 24, 2018 at 12:56 AM
You seem to have trouble to understand what a Quaternion actually is. There are multiple ways to express rotations or orientations in 3d. One common way are Euler angles. Another, completely different way is using a unit quaternion. The components of a quaternion do not represent angles. They are actually resemble a four dimensional complex number.
You almost never want to mess with individual components of a quaternion. It's a normalized complex number and should be treated as "one thing". Unlike euler angles which represent three seperate consecutive rotations around world axes, a quaterion can directly represent any 3d rotation as one "operation". This has many advantages. For example while euler angles can suffer from gimbal lock, quaterions do not have this problem. They also allow direct interpolations between two rotations.
If you want to rotate your object around the y axis you have several options. The easiest one would be to use Transform.Rotate:
transform.Rotate(0, speed * Time.deltaTime, 0, Space.World);
Another solution is to create a relative rotation using either Quaterion.Euler or Quaterion.AngleAxis and rotate the object by this rotation
transform.rotation = Quaterion.AngleAxis(speed * Time.deltaTime, Vector3.up) * transform.rotation;
Note that those examples will rotate around the world up axis. If you want to rotate around the local up vector you would use Space.Self for Rotate and the apropriate axis for AngleAxis which would be transform.up.
It highly depends on your usecase and what method you prefer.
If you are interested in how a quaterion works i recommend this Numberphile video on quaterions
Answer by foxtrot117 · Mar 24, 2018 at 12:47 AM
transform.rotation.eulerAngles = Vector3(floatx,floaty,floatz);
read more here: https://docs.unity3d.com/ScriptReference/Quaternion.html
This doesn't work. A Quaternion is a value type and transform.rotation is a property. Changing the eulerAngles property of the rotation property would only change a temporary copy of the rotation and won't affect the gameobject at all.
Also note that you should avoid doing any relative rotations with eulerangles as the quaternion-to-eulerangles conversion is not unique.
The transform component has a seperate property to get or set the local or worldspace eulerangles (worldspace, localspace).
Your answer
Follow this Question
Related Questions
Problem with normal snapping and rotations. 1 Answer
Rotate vector3 relative to another vector3 1 Answer
Unity's equivalent of Vector3.Transform(...) 2 Answers
Rotate vector towards another vector around perpendicular vector 1 Answer
How do you rotate an object with Vector that has another vector defining its angle 0 Answers