Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Noxury · Apr 14, 2017 at 10:22 PM · cameraquaternioncamera rotatetarget

How to rotate a camera locally around target?

I have seen only examples of camera orbiters where the camera is being rotated on the XZ-plane. But how I can orbit the cam around based from the rotation of the target (Local instead Global Rotation) ?

Hey.. ( ͡° ͜ʖ ͡°)

This is a little visualization I have mashed up from Unity to help you understanding. The camera moves on the sphere, constantly looking at the target (center of the sphere).

Lets assume I want to move my mouse left/right.

On the left side of image:

Normal Behaviour of most cam-orbit scripts, orientating on the green circle parallel to the ground (global Y).

On the right side of image:

This is in what angle the cam should go around. It follows the green circle, which is obviously the Y-Axis of the target.

The same for up/down on the red circle which is representing the targets X-Axis, instead of the global X.


This is my approach so far:

 void LateUpdate () {
     if (!target)
         return;
     x += Input.GetAxis("Mouse X") * xSpeed * distance * Time.deltaTime;
     y -= Input.GetAxis("Mouse Y") * ySpeed * distance * Time.deltaTime;
         
     Quaternion rotation = Quaternion.Euler(y, x, 0) * target.rotation;

     distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*distanceMin*3f, distanceMin, distanceMax);
 
     Vector3 position = -Vector3.forward * distance * rotation + target.position;
 
     transform.rotation = rotation;
     transform.position = position;
 }

I hope this is clear enough to understand ;) Thanks in advance!

camrot.png (48.0 kB)
Comment
Add comment · Show 1
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 Joe-Censored · Apr 14, 2017 at 11:57 PM 0
Share

I have a simple asset on the store that does what you are asking if I understand your question correctly. $$anonymous$$essage me on the Unity forum or send an email to joecensoredgames@gmail.com and I'll send you a free voucher for it. Then you can take a look at what the code does, take it apart, use it as is, whatever you would like.

For some reason Unity answers doesn't have a private message feature or I'd send you the voucher here.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zilppuri · Apr 19, 2017 at 06:14 PM

Would it work if instead of Vector3.forward, you use the targets forward vector?

 Quaternion rotation = Quaternion.Euler(y, x, 0);
 Vector3 position = -target.forward * distance * rotation + target.position;
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

95 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

Related Questions

Rotate Camera/Leaning 1 Answer

Top down camera for object moving on sphere 1 Answer

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

Rotate player to screen pos 0 Answers

Free Orbit Cam and Mouse Aim Cam aren't working together 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