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 foureyes44 · Mar 24, 2018 at 12:06 AM · rotationvector3quaternionconversioncs0029

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

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

2 Replies

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

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

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
avatar image
0

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

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 Bunny83 · Mar 24, 2018 at 01:03 AM 1
Share

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

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

119 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

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


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