Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Anymeese · Oct 25, 2013 at 06:51 AM · rotationquaternionlerp

How to represent a gameobject's (x,y,z) rotation as a quaternion?

I want to lerp a camera's rotation FROM (35,0,0) TO (15,0,0) and have no idea how, because I haven't gotten to Quaternions in math yet :( I also tried

 Camera.transform.rotation.x = MathF.Lerp(35,15,timeToLerp);

but it said I couldn't do that, so I need to use

 Camera.transform.rotation = Quaternion.Lerp(Camera.transform.rotation,???,timeToLerp);

I appreciate the help ^^

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
2
Best Answer

Answer by robertbu · Oct 25, 2013 at 07:02 AM

You don't have to understand Quaternion math to use Quaternions. The class comes with many helper functions. While you can Slerp from one specific rotation to another, it is easier to Slerp from the current rotation to another specific rotation. If you first set the rotation. Here is a script:

 #pragma strict
 
 var speed = 55.0; // Degrees per second;
 private var qFrom = Quaternion.Euler(35,0,0);
 private var qTo = Quaternion.Euler(15,0,0);
 
 function Start() {
     transform.rotation = qFrom;
 }
  
 function Update() {
       transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, speed * Time.deltaTime);
 }
Comment
Add comment · Show 6 · 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 Anymeese · Oct 25, 2013 at 02:31 PM 0
Share

Here's the full code that I have for both camera changes. I ultimately want to zoom in (position) on the camera's parent gameobject, and rotate from (35,0,0) to (15,0,0) to "look down" onto the parent object

Currently, the rotation still isn't changing at all, and the position I$$anonymous$$$$anonymous$$EDIATELY becomes the end position, doesn't gradually move over time. If I ins$$anonymous$$d do timeTakesToZoom * Time.deltaTime for the local position lerp's duration, nothing happens at all

 //swoop in/zoom in over 1 second to see the char
 float timeTakesToZoom = 1f;
 //lerp the original camera's position to the zoomed in RelativePosition
 post$$anonymous$$atchCam.transform.localPosition = Vector3.Lerp(post$$anonymous$$atchCam.transform.localPosition, new Vector3(0,1f,1.2f), timeTakesToZoom);
 //lerp its rotation to the final rotation
 Quaternion fromThis = Quaternion.Euler(35,0,0);
 Quaternion toThis = Quaternion.Euler(15,0,0);
 float degreesPerSecond = (35-15) / timeTakesToZoom;
 post$$anonymous$$atchCam.transform.rotation = Quaternion.RotateTowards(fromThis, toThis, degreesPerSecond * Time.deltaTime);
avatar image robertbu · Oct 25, 2013 at 03:24 PM 0
Share

As mentioned, this is not a from/to use. Your code should be:

 float degreesPerSecond = (35-15) / timeTakesToZoom;
 post$$anonymous$$atchCam.transform.rotation = Quaternion.RotateTowards(post$$anonymous$$atchCam.transform.rotation, toThis, degreesPerSecond * Time.deltaTime);

And you need to set the initial rotation before you start executing this code:

 post$$anonymous$$atchCam.transform.rotation = Quaternion.Euler(35,0,0);

You have to use the current rotation as the first parameter of the RotateTowards(). Note that you can do a FromTo kind of rotation with Quaternion.Slerp(), but then you have to create a timer so that the last parameter cycles from 0.0 to 1.0 over time.

avatar image Anymeese · Oct 25, 2013 at 03:56 PM 0
Share
 //swoop in/zoom in over 3 seconds to see the char
 float timeTakesToZoom = 3f;
 //lerp the original camera's position to the zoomed in RelativePosition
 post$$anonymous$$atchCam.transform.localPosition = Vector3.Lerp(post$$anonymous$$atchCam.transform.localPosition, new Vector3(0,1f,1.2f), timeTakesToZoom)
 //lerp its rotation to the final rotation
 post$$anonymous$$atchCam.transform.rotation = Quaternion.Euler(35,0,0);
 Quaternion toThis = Quaternion.Euler(15,0,0);
 float degreesPerSecond = (35-15) / timeTakesToZoom;
 post$$anonymous$$atchCam.transform.rotation = Quaternion.RotateTowards(post$$anonymous$$atchCam.transform.rotation, toThis, degreesPerSecond * Time.deltaTime);

I tried making the rotation this AND tried using slerp, and neither did anything. And the localPosition lerp is still happening in one frame rather than over 3 seconds... I don't understand this :/

avatar image robertbu · Oct 25, 2013 at 09:36 PM 0
Share

The problem is that the assignment of the original rotation cannot be done at every frame. It needs to be done once at the start of the rotation. Take a careful look at my example code. I set the original rotation in Start(). Then you use RotateTowards() each frame with the current rotation as the first parameter.

avatar image Anymeese · Oct 26, 2013 at 03:23 AM 0
Share

...still not working (I'm so sorry to keep bothering you. Really do appreciate the help)

I added this to my Update()

 if(movingCamForVicDance)
 {
        float timeTakesToZoom = 3f;
        Quaternion toThis = Quaternion.Euler(15,0,0);
        float degreesPerSecond = (35-15) / timeTakesToZoom;
        post$$anonymous$$atchCam.transform.rotation = Quaternion.RotateTowards(post$$anonymous$$atchCam.transform.rotation, toThis, degreesPerSecond * Time.deltaTime);
 }

and changed my IEnumerator to this:

  float timeTakesToZoom = 3f; //IF YOU CHANGE THIS, ALSO CHANGE IT AT THE BOTTO$$anonymous$$ OF UPDATE
  post$$anonymous$$atchCam.transform.localPosition = Vector3.Lerp(post$$anonymous$$atchCam.transform.localPosition, new Vector3(0,1f,1.2f), timeTakesToZoom);
  movingCamForVicDance = true;
  yield return new WaitForSeconds(timeTakesToZoom);
  movingCamForVicDance = false;

so that the rotateTowards is called in Update ins$$anonymous$$d of the IEnumerator, its first parameter is set to the gameobject's rotation, and the initial rotation (35) is now assigned in Start()

But STILL the position lerp just immediately puts the camera at the resulting positionr ather than panning over the duration, and the camera doesn't rotate whatsoever

Show more comments
avatar image
1

Answer by Bunny83 · Oct 25, 2013 at 07:02 AM

First of all when you use Camera.transform.rotation as start value you actually don't LERP anymore. It becomes a decellerated rotation. Your Mathf example does lerp because start and end value have to be constant for a linear interpolation.

To answer your ??? question:

     Quaternion.Euler(15,0,0)

This creates a rotation out of the given eulerangles. Quaternions are better to represent rotations since they avoid a Gimbal lock

If you want to lerp between two rotation you have to store them both in a variable and use those to lerp. It depends on what you want.

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

16 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

Related Questions

Use Lerp to rotate object 2 Answers

How can I smooth the rotation using quaternion and raycast? 1 Answer

better way to rotate instead Coroutine 1 Answer

Smoothing the characters rotation with Lerp 2 Answers

Quaternion lerp is not rotating my object to the desired rotation? 1 Answer


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