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 Ostinyo · Sep 15, 2013 at 06:54 AM · quaternioncamera rotateroll

Roll by rotating camera past 360 degrees on x axis

The idea here is simple: I need to rotate the camera to provide the effect of rolling in first person. My major concern is that I want to rotate the camera on the x axis beyond 360 degrees. I am using Quaternion.Lerp for rotating.

 //Inside the update function
 EndAngle = Quaternion.Euler(5, 0, 0);
 StartAngle = Quaternion.Euler(25,0,0);
 
 if (tParam < 1)
 {
   tParam += Time.deltaTime * lerpSpeed;
   Camera.main.transform.rotation = Quaternion.Lerp(StartAngle, EndAngle, tParam);
 }
 
 if (tParam >= 1)
 {
   tParam = 0;
 }

Here is working code I am using to rotate the camera from 25 to 5 degrees. Of course, I want to start at 5 and end at 365(5). I would however like the lerp to start at whatever the current rotation angle on the x axis is for the camera, but am wary that using StartAngle = Camera.main.transform.rotation.x will result in the StartAngle updating constantly. How should I go about taking the starting angle of the camera in an efficient manner?

tl;dr How should I make the camera rotate to 365 degrees? (And should I use slerp to make the roll more realistic?) Thanks for any help in advance :)

Comment
Add comment · Show 8
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 vexe · Sep 15, 2013 at 07:00 AM 0
Share

"but am wary that using StartAngle = Camera.main.transform.rotation.x will result in the StartAngle updating constantly." - Don't worry about that, transform.rotation is a Quaternion, a Quaternion is a struct, not a class (a value-type, not a reference type) - meaning when you do startAngle = camera.main.transform.rotation; startAngle now is a copy of the camera's rotation (when you modify the copy, you won't affect the original)

avatar image Ostinyo · Sep 15, 2013 at 07:07 AM 0
Share

Thanks for that quick reply! So StartAngle wouldn't keep updating with the new value? (It's in the update/ if(is rolling) condition)

avatar image vexe · Sep 15, 2013 at 07:10 AM 0
Share

What I meant to say is:

 startAngle = something;
 print(startAngle); // gets X
 print(something);  // gets y
 changeToY(something);
 print(startAngle); // still gets X
 print(something);  // gets y

If you keep changing/assigning your startAngle in your update each frame to something new, of course it will change :)

avatar image Ostinyo · Sep 15, 2013 at 07:19 AM 0
Share

Ok, so should I try a check to see if I have gotten the rotation.x value? Like this:

 Update ()
 {
   if(!findRot)
   {
     StartAngle.x = Camera.main.transform.rotation.x;
     findRot = true;
   }
 }

Or is there a more efficient way?

avatar image vexe · Sep 15, 2013 at 08:32 AM 0
Share

I am terribly sorry for the delay, I've been messing around with it all this time, I really suck at rotations! - But I've managed to get a smooth rolling going on for the camera using $$anonymous$$athf.$$anonymous$$oveTowardsAngle - It rotates smoothly, but for some reason for me it just keeps on rotating :D I'm sure you'll use it better than me - You mentioned the x-axis, you want to roll forward and backward? - I thought you were doing a side roll, that way you have to rotate around your cam's local z. Here's a good tut about rotations, and here's a very useful tool about rotations that you might be interested in. I am sorry I couldn't provide you with better assistance. @ArkaneX requesting backup.

Show more comments

1 Reply

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

Answer by robertbu · Sep 18, 2013 at 05:43 AM

As you've found out, Quaternions are lazy...they always take the shortest distance path between two rotations. This means that you cannot do a rotation of more than 180 degrees with a single setup. One solutions is to use Vector3.Lerp() and assign the results to Transform.eulerAngles. There are two issues with using this method:

  1. Vector3.Lerp() does not handle the 360/0 boundary, so you have to change the representation of the values to something that will be incremented and decremented the way you want. So if you wanted to start at 25 and end at 5 the long way, you could specify the start as 25 and the end at 365. Or you could specify the start as -335 and the end as 5.

  2. You must never read Transform.eulerAngles. You need to maintain your own Vector3 representation of the angle and treat Transform.eulerAngles as write-only. If you were to read back transform.eulerAngles after setting it, you often find the internal representation different than the value you just set. As long as you only set eulerAngles, the rotation will work.

Comment
Add comment · Show 3 · 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 Ostinyo · Sep 18, 2013 at 05:51 AM 0
Share

Thanks for the answer, I'm going to try using Vector3.Lerp and see how it goes. I had already thought of this solution, but I didn't want to bother with converting rotations beyond 360 degrees (though I suppose it won't be too bad?).

Here's my thought. Using your method, I can lerp the rotation to 365 or so. Then, once the lerp has finished, I could set the rotation to 5 degrees (or whatever respective angle) to compensate for wrap around 360.

avatar image robertbu · Sep 18, 2013 at 05:59 AM 0
Share

Changing your representing after the rotation will work just fine. You can use any representation you want and change from one representation to another and everything will work just fine. You could also pick representation that did not require you to normalize the rotation (like start at 3600 for a 0 rotation). As mentioned, the only caution is to not read eulerAngles.

avatar image Ostinyo · Sep 18, 2013 at 06:20 AM 0
Share

It is a fair point that I can use crazy huge representations for angles, because if I don't directly modify the rotation but just add to the rotation variable (e.g. EndRot = CurRot + 360) I could continue to correctly lerp the values even if they're not normalized. The only problem would be keeping the boundaries of the rotating camera affect while running (because if I start from a low rotation the boundaries will be lowered by the amount of deviation). For that reason, I'm going to stick to normalizing the result, but thanks for the insight! (and the read warning)

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

Top down camera for object moving on sphere 1 Answer

Finding roll angle 0 Answers

Rotate player to screen pos 0 Answers

Moving FPS/TPS camera to look at a target and resuming player control 1 Answer

Rotating camera with 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