• 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 DoubleS · Jan 03, 2013 at 05:58 AM · camerarotationdegree

Minus Degree Rotation

I'm working on a camera script and i want to make that when i'm moving to the left, camera turns to 90 degree left and when i'm moving right, camera turns 90 degree right(from the back of the character). I'm not copying the full script:

 var horizontal = Input.GetAxis("Horizontal");


 else if(horizontal == 1 || horizontal == -1){
             Debug.Log(Input.GetAxis("Horizontal"));
             wantedRotationAngle = 90;
             wantedRotationAngle *= horizontal;
             
             currentRotationAngle = Mathf.Lerp(currentRotationAngle, wantedRotationAngle, Time.deltaTime * rotationDamping);
             
             currentHeight = Mathf.Lerp(currentHeight, wantedHeight, Time.deltaTime * heightDamping);
             
             var currentHRotation = Quaternion.Euler(0, currentRotationAngle, 0);
             
             
             transform.position = target.position;
             transform.position -= currentHRotation * Vector3.forward * distance;
             
             transform.position.y = currentHeight;

And the problem is: When i'm moving to the left(horizontal = -1) it tries to turn 270 degrees. And no problem with right. How can i make it turn 90 degrees opposite direction?

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 Piflik · Jan 03, 2013 at 10:30 AM

Try lerping between two Quaternions instead of numbers. Floats will lerp between 0 and 270 only in positive direction, since they are not circular. Quaternions will use the shortest distance between two rotations.

Something like this:

 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(0, wantedRotationAngle, 0), Time.deltaTime * rotationDamping);
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 DoubleS · Jan 03, 2013 at 02:35 PM 0
Share

Well thanks, it worked, but; is there any way i can use Quaternion.Lerp without converting every variable i used, to quaternion? Because this solution comes with additional 3 lines for me to convert degrees like this:

var quatWantedRotationAngle = Quaternion.Euler(0, wantedRotationAngle, 0);

var quatCurrentRotationAngle = Quaternion.Euler(0, currentRotationAngle, 0);

var QLCurrentRotationAngle = transform.rotation;

and then:

QLCurrentRotationAngle = Quaternion.Lerp(quatCurrentRotationAngle, quatWantedRotationAngle, Time.deltaTime * rotationDamping);

avatar image
1

Answer by CodeMasterMike · Jan 03, 2013 at 08:44 AM

I think the issue is that your Lerp rotation calculates from 0 to 270 (meaning 0, 1, 2, 3 etc until 270).

What you need to do, is do a calulation that "subtracts" instead of adding (meaning 0, 359, 358, 357 etc until 270). I don't think you can do something like that with Lerp in a easy way.

Good luck!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Quaternion Slerp Sanity Check 1 Answer

How to freeze the rotation or the position on the camera transform? 1 Answer

how do i make first person character rotate left and right along with camera? 0 Answers

Override Oculus Orientation 5.3 1 Answer

How to make the camera rotate only when right click is held down on the mouse? 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