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 AJ37z · Jun 21, 2017 at 12:09 PM · rotationcamera-lookrayrotation axisscreenpointtoray

Using Camera screen point to ray for look rotation problem...

Hey there, I have been trying to solve this problem forever now. I'm trying to rotate the main camera using a ray that originates from itself with mouse position as input. Then I use that rays direction to make a look rotation for the camera, that way the camera rotates towards the current mouse position at a certain speed (using lerp as well) until its looking / pointing to the current mouse position.

Here's the code:

 Ray mousePos = Camera.main.ScreenPointToRay(Input.mousePosition);
 Vector3 lerpToMousePos = Vector3.Lerp(transform.forward, mousePos.direction, 2 * Time.deltaTime);
 transform.rotation = Quaternion.LookRotation(lerpToMousePos); 


The problem occurs when I try rotating the camera +-90 degrees on the x-axis, it starts spinning like crazy. I want the camera to be able to rotate all the way around on the x-axis freely.

Here's what happens when I try to rotate the camera to more than 90 degrees on the x-axis:

alt text

https://gyazo.com/626a8b16b8e31b995966778a86a9e344

In this gif the green drawn ray in the scene just represents the cameras transform.forward / direction / current rotation. The red ray is the one coming from the camera towards the mouse current position (mousePos ray). If you know how to maybe solve this in the slightest (or know a possible alternative solution) please respond to this, I have been trying to find the cause of this for too long. Thanks for your time!

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

Answer by FlaSh-G · Jun 21, 2017 at 12:29 PM

Quaternion.LookRotation can take a second vector that defines where "up" is. Since you do not do that, it just takes Vector3.up, so as soon as you hit or even dangle around Vector3.up as forward vector, this can happen.

In your case, it might be enough to pass the current transform.up vector as up. Since it rotates with the object each frame, it's going to make the object's up stay as much the same as possible.

 transform.rotation = Quaternion.LookRotation(lerpToMousePos, transform.up); 
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 AJ37z · Jun 21, 2017 at 12:38 PM 0
Share

Oh wow that was very easy, thanks!

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

84 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

Related Questions

Rotate objects face to camera 1 Answer

How to find or compute or look for overall/max RPM by simply checking for rotation? 1 Answer

Quaternion LookRotation issue 0 Answers

How to rotate a quad 1 Answer

Rotate object of certain degree on button press 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