- Home /
Error cannot convert quaternion to vector3
I am a noob developer, and i'm trying to rotate a cube on the y axis (only 90 degrees once then i want all rotations to stop, NOT 90 degrees every second) using lerp. When i use the code that i do, I get an error saying i cannot convert an unityengine.quaternion to a unityengine.vector3. I get this error on lines 11, 16, and 17. If anyone would be able to help me, that would be amazing, and I would be immensely appreciative. Here's my code:
using UnityEngine; using System.Collections;
public class rotatecube : MonoBehaviour { private Vector3 newPos;
void Start()
{
newPos = transform.rotation;
}
void hiccup()
{
Vector3 posA = Quaternion.Euler(0, 0, 0);
Vector3 posB = Quaternion.Euler(0, 90, 0);
if (Input.GetKeyDown (KeyCode.A))
{
newPos = posA;
}
if (Input.GetKeyDown (KeyCode.Y))
{
newPos = posB;
}
transform.rotation = Vector3.Lerp (transform.rotation, newPos, Time.deltaTime);
}
void Update()
{
hiccup ();
}
}
Answer by jenci1990 · Nov 23, 2018 at 09:54 PM
void Start() {
newPos = transform.rotation.eulerAngles;
}
void hiccup() {
Vector3 posA = new Vector3(0, 0, 0);
Vector3 posB = new Vector3(0, 90, 0);
if (Input.GetKeyDown(KeyCode.A)) {
newPos = posA;
}
if (Input.GetKeyDown(KeyCode.Y)) {
newPos = posB;
}
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(newPos), Time.deltaTime);
}
void Update() {
hiccup();
}
This worked, it only gave me one warning. !!! thanks a ton!!
Answer by tristantcate · Nov 23, 2018 at 07:21 PM
Hi,
transform.rotation always returns a Quaternion. In your Start() method, you're trying to push a Quaternion into a Vector3 and later on the opposite. To change this simply change
void Start()
{
newPos = transform.rotation;
}
Into
void Start()
{
newPos = transform.rotation.eulerAngles;
}
After that on line 21 change this
transform.rotation = Vector3.Lerp (transform.rotation, newPos, Time.deltaTime);
to this.
transform.rotation.eulerAngles = Vector3.Lerp (transform.rotation.eulerAngles, newPos, Time.deltaTime);
If you look closely when you hover over a function like Vector3.Lerp you get to see what it returns.
In this case it says "Vector3 Vector3.Lerp" etc. Keep this in mind when assigning something to a variable that might not have the same container type. Same goes for transform.rotation.
You can see how this is a quaternion by hovering over it as well.
I hope this helped! :)
This works fixing the first problem, but for the second problem : transform.rotation.eulerangles = Vector3.Lerp (transform.rotation, newPos, Time.deltaTime);
i still get an error saying cannot convert quaternion to vector3, so i decided to do this:
transform.rotation.eulerangles = Vector3.Lerp (transform.rotation.eulerangles, newPos, Time.deltaTime)
and i get an error that says: " cannot modify the return value of 'unityEngine.Transform.rotation' because it is not a variable"
@tristantcate or anyone if you can pls help or explain this to me i'd be extremely grateful!!
Ah my bad I should've looked through your code a bit better. You assign newPos by giving it either posA or posB. These however are set as Quaternions since they are both Quaternion.Euler, which results in a Quaternion based on the value you put in between the parenthesis.
There are two ways to go about this. For now let's keep it simple and just input the posA and posB as Vector3s.
Change
Vector3 posA = Quaternion.Euler(0, 0, 0);
Vector3 posB = Quaternion.Euler(0, 90, 0);
to
Vector3 posA = new Vector3(0, 0, 0);
Vector3 posB = new Vector3(0, 90, 0);
Hope that helped.
Also well spotted that line 21 also needed transform.rotation.eulerAngles ins$$anonymous$$d of just transform.rotation. I totally missed that one! Edited for the sake of convenience.
Answer by Hirnwirbel · Nov 23, 2018 at 06:31 PM
Quaternion.Euler gives you a quaternion (Unity's internal way of calculating rotations) based on the 3 Euler angle values you put into it. A Quaternion is not the same thing as a Vector 3, which is why Unity throws an error here.
But, you don't need to use a Quaternion in your example, you can just directly create a Vector3. Like this:
Vector3 posA = new Vector3 (0, 0, 0);
Vector3 posB = new Vector3 (0, 90, 0);
after changing the quaternions to vector 3's on lines 8 and 9, i still get an error on lines 3 and 21. on line 3 the error says cannot convert quaternion into a vector 3, and then on line 21 it has the same probelm. @Hirnwirbel . pls help I would be very appreciative.
Answer by $$anonymous$$ · Nov 24, 2018 at 02:40 AM
Quaternion and Vector3 are different But, you assign Quaternion directly to Vector3