- Home /
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?
Try making the camera a child of the sphere object and use the same script.
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.
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.
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.
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?
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); }
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);
}
}
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);
}
}
Actually, you need to change the Camera's position, not rotation... I'll need to think about it some more.
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
Follow this Question
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