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
2
Question by The 3D Ninja · Dec 29, 2010 at 04:36 PM · camerarotationmousedirectionlookat

Adjust camera rotation while looking at a target

Hi,

I currently have my camera looking at a target using LookTo(). I need to be able to adjust the rotation of the camera based off mouse mouse movement. Basically, I want the camera to look at a target, but the user can move the mouse to look around that target. I don't want the camera to move, just rotate a certain degree to the left/right, up/down, while still facing the same target.

Here's what I have to rotate the camera based off mouse movement. This works for rotation based off mouse move, but since I'm setting the lookTo towards the mouse, I'm losing the desired camera direction.

I'd greatly appreciate some help! Thanks.

void RotateCamera(Vector2 input) {

     if(isRotateCamera)//only rotate if camera is not moving along path
     {
     Vector3 MouseWorldPosition = camera.ScreenToWorldPoint(new Vector3(input.x, input.y, 100));
     //apply rotation to camera

     MouseWorldPosition.Normalize();

     transform.LookAt(MouseWorldPosition * 20, transform.position);
     transform.rotation = Quaternion.Euler(new Vector3(transform.eulerAngles.x, transform.rotation.eulerAngles.y, 0));

     }
 }

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 The 3D Ninja · Dec 29, 2010 at 09:15 PM

Found a solution. I implemented the MouseLook code from the standard assets into my class. Here's the method that is called to rotate the camera.

void RotateCamera(Vector2 input) {

     if(isRotateCamera)//only rotate if camera is not moving along path
     {

     if (axes == RotationAxes.MouseXAndY)
     {
         float rotationX = transform.localEulerAngles.y + input.x * sensitivityX;

         rotationY += input.y * sensitivityY;
         rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

         transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
     }
     else if (axes == RotationAxes.MouseX)
     {
         transform.Rotate(0, input.x * sensitivityX, 0);
     }
     else
     {
         rotationY += input.x * sensitivityY;
         rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

         transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
     }



     }
 }

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

No one has followed this question yet.

Related Questions

Quaternion.LookRotation is making my object rotate around a point? 1 Answer

How to setup camera look at mouse on right click 0 Answers

Camera Rotation 1 Answer

LookAt Camera over 90 degree 1 Answer

Third person animation 0 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