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 ();
}

}

4 Answers

4

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.

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. ![128237-quaternionsandv3s.png|490x66](upload://dxpAx6q5mEu7YvZsYEpLPvuQCWe.png)

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.
128238-quaternionsandv3s2.png

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!!

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!!

Quaternion and Vector3 are different
But, you assign Quaternion directly to Vector3

It's an Image