Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jenniferwalsh · Nov 23, 2018 at 06:16 PM · errorvector3quaternionlerpconvert

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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();
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jenniferwalsh · Nov 24, 2018 at 02:21 AM 0
Share

This worked, it only gave me one warning. !!! thanks a ton!!

avatar image
0

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. alt text
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. alt text
You can see how this is a quaternion by hovering over it as well.


I hope this helped! :)


quaternionsandv3s.png (2.6 kB)
quaternionsandv3s2.png (2.7 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jenniferwalsh · Nov 23, 2018 at 07:47 PM 0
Share

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

avatar image tristantcate jenniferwalsh · Nov 23, 2018 at 11:58 PM 0
Share

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.

avatar image
0

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jenniferwalsh · Nov 23, 2018 at 06:45 PM 0
Share

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.

avatar image
0

Answer by $$anonymous$$ · Nov 24, 2018 at 02:40 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

149 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with Quaternions and Vector3s 2 Answers

Vector3.MoveTowards and Quaternion.RotateTowards 1 Answer

How to smoothly rotate to certain directions using input axis 1 Answer

combine / convert rotations (Quaternion and Vector3) 1 Answer

How would I smooth out a Quaternion? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges