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
0
Question by felipopulus · Nov 20, 2014 at 11:19 PM · camerarotatesnapboo

Rotate around object at a given angle

Hello, new to Unity here. Looked around everywhere but could't find and answer. Thanks in advance for the help. I'm trying to rotate a camera around and object as I drag the mouse. The more I drag the mouse the faster it rotates. I don't want it to rotate smoothly, I want it to rotate on increments of 45 degrees. I don't know how to find the camera position values at a given angle. (I cannot parent the camera to the object and rotate the object -- would require days of rewriting camera scripts). I DO know how to round/snap individual attributes after I find the correct position at a given angle. Here is the desired behavior except that it doesn't snap: (written in boo)

 import UnityEngine
 
 class cameraRotation (MonoBehaviour): 
     
     public object_to_rotate_around as Transform
     private main_cam as Camera
     private initial_mouse_click as Vector3
     
     def Start ():
         main_cam = Camera.main
         //initial_mouse_click = Input.mousePosition
     
     def Update ():
         if Input.GetMouseButtonDown(0):
             initial_mouse_click = Input.mousePosition
         elif Input.GetMouseButton(0):
             mouse_delta = Input.mousePosition - initial_mouse_click
             main_cam.transform.RotateAround(object_to_rotate_around.position, Vector3.up, mouse_delta.x * 0.05)
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
Wiki

Answer by richyrich · Nov 21, 2014 at 01:34 AM

Hi. I'm not a Boo programmer, so please excuse the syntax errors ;) Basically you want to look for the modulus operator and apply. Change:

     elif Input.GetMouseButton(0):
          mouse_delta = Input.mousePosition - initial_mouse_click
          main_cam.transform.RotateAround(object_to_rotate_around.position, Vector3.up, mouse_delta.x * 0.05)

to

     elif Input.GetMouseButton(0):
          mouse_delta = Input.mousePosition - initial_mouse_click
         mouse_delta.x *= 0.05f;
         stored += mouse_delta.x;
         rem = stored % 22.5f;
         actualChange = stored-rem;
         stored = rem;
          main_cam.transform.RotateAround(object_to_rotate_around.position, Vector3.up, actualChange )

//You need to also declare float stored = 0 and float actualChange = 0; at the op of your program

For reference, here's my test code in c# which I used to check it would work. Obviously my cube and cylinder will not work unless they're in your project ;)

 Vector3 initial_mouse_click=Vector3.zero;
 float stored = 0;

 void Update () {  

     if (Input.GetMouseButtonDown(0))
     {
         initial_mouse_click = Input.mousePosition;
     }
     else if (Input.GetMouseButton(0))
     {
         Vector3 mouse_delta = Input.mousePosition - initial_mouse_click;
         GameObject go = GameObject.Find("Cylinder");
         GameObject cb = GameObject.Find("Cube");
         mouse_delta.x *= 0.05f;
         stored += mouse_delta.x;
         float rem = stored % 22.5f;
         float actualChange = stored-rem;
         stored = rem;
         go.transform.RotateAround(cb.transform.position, Vector3.up, actualChange);//mouse_delta.x);
     }
 }

I apologise if I've used too many variables. It's 1am, I'm tired and just don't care anymore ;)

Edit: Rearranged code to retain data lifetime

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
avatar image
0

Answer by felipopulus · Nov 21, 2014 at 05:06 AM

This works great! thank you!

This code should be outside of the update function:

 Vector3 initial_mouse_click=Vector3.zero;
 float stored = 0;
 float actualChange = 0;
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 richyrich · Nov 21, 2014 at 12:00 PM 0
Share

Updated the answer thanks. The float actualChange can still be declared local, because it's value does need to be retained. The answer could now be accepted

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help with camera snap 1 Answer

Object rotates with camera 2 Answers

Rotating Camera on Touch 1 Answer

Camera won't rotate with parent object(Player) 2 Answers

Rotate camera smoothly to certain degrees with iphone touch 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