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 zero_null · May 23, 2018 at 07:26 AM · rotationquaternionsmoothly

Make Camera smoothly orbit around my object with mouse/touch input but with limit on z-axis

I am making kind of a showroom where when I drag with mouse/touch, I want my camera to smoothly rotate (kind of orbit) around my main game object. What I have done so far is, that I have my camera parented by an empty game object (with required rotation) and script with following code is attached to the parent object of my camera.

 public float smoothness;
 private float xRot, yRot;
 const string MOUSE_X = "Mouse X", MOUSE_Y = "Mouse Y";
 float mult = 3;
 void Update()
 {
     if (Input.GetMouseButton(0))
     {
         xRot += Input.GetAxis(MOUSE_X) * mult;
         yRot -= Input.GetAxis(MOUSE_Y) * mult;
     }
     xRot = Mathf.Lerp(xRot, 0, smoothness);
     yRot = Mathf.Lerp(yRot, 0, smoothness);
     transform.eulerAngles += new Vector3(0, xRot, yRot);
 }

My problem is that I can't find a way to restrict rotation on the z-axis. I have seen allot of posts on different forums but still didn't get. I actually have tried the following code as well but it is not smooth(kind of a little continued rotation after input is removed with lerp)

 Quaternion minRot = Quaternion.Euler(0, 0, -10f);
 Quaternion maxRot = Quaternion.Euler(0, 0, 35f);
 float mult = 30;
 void Update()
 {
     if (Input.GetMouseButton(0))
     {
         xRot += Input.GetAxis(MOUSE_X) * mult;
         yRot -= Input.GetAxis(MOUSE_Y) * mult;
     }
     transform.rotation = Quaternion.Euler(0, transform.rotation.y + xRot, Mathf.Clamp(transform.rotation.z + yRot, -5, 30));
 }

When I try using Quaternion.Lerp then it sometimes kind of stuck (I think it's the gimble lock issue?) and it also starts rotating on the x-axis (which isn't required).

 transform.rotation = Quaternion.Lerp(transform.rotation,
    Quaternion.Euler(0, transform.rotation.y + xRot, Mathf.Clamp(transform.rotation.z + yRot, -5, 30)), 0.5f);

I know, I am not well versed with Quaternion and I have read that the individual euler angle axis shouldn't be modified but how can I have a smooth rotation? Any help will be appreciated.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by anthot4 · May 23, 2018 at 09:48 AM

Your not far off. You need to clamp the z axis roation just before you apply set it to the objects euler angles.

Comment
Add comment · Show 7 · 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 zero_null · May 23, 2018 at 11:20 AM 0
Share

not quite really because clamping euler angle to a value of -10 means 360-10, which will be an incorrect value.

avatar image anthot4 zero_null · May 23, 2018 at 11:27 AM 0
Share

No, you clamp it from 0-360 and if rotation tries to go below 0 you set it to 360 and if the rotation goes above 360 you set it to 0. It does work as I have got it working well in my game.

avatar image anthot4 anthot4 · May 23, 2018 at 11:27 AM 0
Share

Also, if your rotating its better to use slerp rather than lerp.

Show more comments
Show more comments

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

116 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

Related Questions

Smoothly rotate Quaternion using random range 1 Answer

Pinball Flipper using Quaternion.Lerp? 1 Answer

Rotate with raw gyro data. 0 Answers

Simple Rotation 1 Answer

i have rotated an gameobject but the axis of the object did not change 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