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 kulesz · Nov 29, 2013 at 03:29 PM · camerarotatemathsphereball

Rotating camera around the sphere

I've found a script that allows to rotate sphere with the mouse: using UnityEngine; using System.Collections;

 public class SphereRotation : MonoBehaviour
 {
     bool hasGrabbedPoint = false;
     Vector3 grabbedPoint;
 
     void Update()
     {
         if (Input.GetMouseButton(0))
         {
             if (!hasGrabbedPoint)
             {
                 hasGrabbedPoint = true;
                 grabbedPoint = getTouchedPoint();
             }
             else
             {
                 Vector3 targetPoint = getTouchedPoint();
                 Quaternion rot = Quaternion.FromToRotation(grabbedPoint, targetPoint);
                 transform.localRotation *= rot;
             }
         }
         else
             hasGrabbedPoint = false;
     }
 
     Vector3 getTouchedPoint()
     {
         RaycastHit hit;
         Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit);
 
         return transform.InverseTransformPoint(hit.point);
     }
 }

However the thing I'm trying to achieve is a bit opposite - I'd like to rotate camera around the sphere, keeping the object still. Could anyone help me a little bit with this?

Comment
Add comment · Show 6
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 Tomer-Barkan · Nov 29, 2013 at 03:50 PM 0
Share

Try making the camera a child of the sphere object and use the same script.

avatar image kulesz · Nov 29, 2013 at 04:10 PM 0
Share

I tried this at the beginning, but it doesn't work - camera rotates chaotically, very fast. I guess it has to do something with the fact, that I'm casting a ray from camera, that is rotating, but I'm not sure.

avatar image Tomer-Barkan · Nov 29, 2013 at 04:23 PM 0
Share

Hmm.. I wouldn't expect it does because the object also rotates with the camera...

Ok, please explain how you want the camera rotation to behave, considering that it's not a sphere you're rotating, but you're current position.

avatar image kulesz · Nov 29, 2013 at 04:27 PM 0
Share

Well, with the script above I can rotate sphere and - with a bit of practice - I can rotate it around its every axis (which I consider very useful). This is more or less exactly the way like Catia camera works and I'd like to write something similar.

In theory it seems easy, I'd like to keep the sphere s$$anonymous$$dy while applying rotation to the camera. Exactly something that I would expecting from attaching camera as child - but - as I said, it doesn't work.

avatar image robertbu · Nov 29, 2013 at 04:38 PM 0
Share

How do you want to drive the rotation. Do you want to do as above and rotate the camera to look at a specific point, or are you going to use some other input to rotate the camera? And do you want an immediate rotation or a rotation over time? And if over time, should the rotation be eased?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Nov 29, 2013 at 05:18 PM

Here is a solution. It is based on mouse movement as you indicated in your comment. To use it:

  • Go to Project Settings > Input and set 'Type' of movement to 'Mouse Movement' for both 'Horizontal' and 'Vertical'.

  • Position the camera over the sphere looking at the sphere at the correct distance from the sphere.

  • Attach the following script to the camera.

  • Drag and drop the sphere onto the 'sphere' variable in the scrip in the Inspector.


    pragma strict

    public var sphere : Transform; public var speed = 1.0;

    private var center : Transform;

    function Start() { center = new GameObject().transform; center.parent = sphere; center.position = Vector3.zero; transform.parent = center; }

    function Update() { // if the sphere moves, uncomment the following line // center.position = sphere.position;

       if (Input.GetMouseButton(0))
             center.Rotate(-Input.GetAxis("Vertical") * speed, Input.GetAxis("Horizontal") * speed, 0.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 sushanta1991 · Jun 27, 2017 at 07:00 AM 0
Share

Thanks for the solution @robertbu here is a working c# code. attach this to your camera and assign the sphere to sphere variable in editor.

 public class SphereRotation : $$anonymous$$onoBehaviour {
 
 
     public Transform sphere; 
     public float speed = 1.0f;
 
     private Transform center;
 
     void Start() { 
         center = new GameObject().transform; 
         center.parent = sphere; 
         center.position = Vector3.zero; 
         transform.parent = center; 
     }
 
     void Update() { 
         // if the sphere moves, uncomment the following line 
         // center.position = sphere.position;
 
         if (Input.Get$$anonymous$$ouseButton(0))
             center.Rotate(-Input.GetAxis("$$anonymous$$ouse Y") * speed, Input.GetAxis("$$anonymous$$ouse X") * speed, 0.0f);
     }
 }
avatar image
0

Answer by Tomer-Barkan · Nov 29, 2013 at 04:51 PM

Ok, so the idea is to rotate the camera together with the sphere, but you want to keep it from going crazy, so after rotating, you have to make sure that the mouse is touching exactly the same point it did before rotating, otherwise without moving the mouse any more, it will rotate again, because the grabbed position will appear to move even though you did not move the mouse.

So, what you could do, is keep the world position where the ray hit the sphere, and compare it to the world position where the ray is hitting the sphere at the moment. Then you'll have to calculate the way the sphere should rotate, in order for the original point of contact to move to the new point of contact.

I think the following script should work:

Edit: Changed it so that the camera moves and not rotates.

 public class SphereRotation : MonoBehaviour
 {
     public Camera rotatingCamera;
     bool hasGrabbedPoint = false;
     Vector3 grabbedPoint;
  
     void Update()
     {
         if (Input.GetMouseButton(0))
         {
             if (!hasGrabbedPoint)
             {
                 hasGrabbedPoint = true;
                 grabbedPoint = getTouchedPoint();
             }
             else
             {
                 Vector3 targetPoint = getTouchedPoint();
                 Quaternion rot = Quaternion.FromToRotation(grabbedPoint, targetPoint);
                 
                 // you need to rotate the camera around the center of the sphere, the opposite of rot        
                 Vector3 toCamera = rotatingCamera.transform.localPosition;
                 toCamera = rot.Inverse() * toCamera;
                 rotatingCamera.transform.localPosition = toCamera;
                 
                 // make camera look at center of sphere
                 rotatingCamera.transform.LookAt(transform.position);
             }
         }
         else
             hasGrabbedPoint = false;
     }
  
     Vector3 getTouchedPoint()
     {
         RaycastHit hit;
         Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit);
  
         return transform.InverseTransformPoint(hit.point);
     }
 }
Comment
Add comment · Show 2 · 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 Tomer-Barkan · Nov 29, 2013 at 04:55 PM 0
Share

Actually, you need to change the Camera's position, not rotation... I'll need to think about it some more.

avatar image Tomer-Barkan · Nov 29, 2013 at 05:05 PM 0
Share

Fixed to move camera. The idea is to rotate the vector pointing from the center of the sphere to the camera the inverse of the rotation that would happen to the sphere in your original script. So basically ins$$anonymous$$d of rotating the script we are rotating the camera. The answer is based on the camera being a child of the sphere, and the script attached to the sphere, otherwise you'll need to do some tweaks.

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

19 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

Related Questions

Rotate a sphere in the same direction the camera is pointing at 2 Answers

Camera follow rotating ball 2 Answers

How to rotate relative to camera angle/position 5 Answers

Need help with the physics and angles of gerbils in a plastic ball.. 2 Answers

How can i keep this button pressed without acctualy doing it ? 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