- Home /
Rotating Sphere With Mouse
I would like to use the mouse to "grab" a point on a stationary positioned sphere and then be able to rotate the sphere by dragging the mouse around. This is similar to the earth in Google Earth.
The below code, attached to a sphere with a mesh collider partially works, but the initial grab jumps to a random place.
using UnityEngine; using System.Collections;
public class MikesTrackBall : MonoBehaviour {
bool isGrabbed = false;
Vector3 grabbedPoint;
void Update ()
{
RaycastHit hit;
if (Input.GetMouseButton(0))
{
if(!isGrabbed)
{
isGrabbed = true;
//Find the hit point
Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit);
//Convert touched point to a local point on the earth
grabbedPoint = getTouchedPointLocal();
}
else
{
Vector3 targetPoint = getTouchedPoint();
transform.rotation = Quaternion.FromToRotation (grabbedPoint,targetPoint);
}
}
else
isGrabbed = false;
}
Vector3 getTouchedPoint()
{
RaycastHit hit;
//Find the hit point
Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit);
//Convert touched point to a local point on the earth
return hit.point-transform.position;
}
Vector3 getTouchedPointLocal()
{
return transform.InverseTransformPoint(getTouchedPoint());
}
}
Answer by Mike Miller · Jul 13, 2010 at 09:51 PM
I posted a solution to this after help from cncguy on in this post: http://answers.unity3d.com/questions/14919/rotate-point-a-on-sphere-to-point-b-on-sphere
Answer by AnaRhisT · Jun 30, 2010 at 11:37 PM
Hey there, When u press right click with the mouse it rotates towards the Sphere or whatever gameobject u want, It's an edited code of MouseLook from Standard Assets. This is the part that does the effect :
if(Input.GetButton("Fire2")){
x += Input.GetAxis("Mouse X") * 250.0f * 0.02f;
y -= Input.GetAxis("Mouse Y") * 500.0f * 0.02f;
}
using UnityEngine; using System.Collections; /// edited by Creative from unity for a RPG camera.(Creative is me - AnaRhisT) [AddComponentMenu("Camera-Control/Mouse Look")] public class MouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationX = 0F;
float rotationY = 0F;
public int yMinLimit = -20;
public int yMaxLimit = 80;
public Transform target;
Quaternion originalRotation;
private float x = 0.0f;
private float y = 0.0f;
public float distance = 3;
// Vector3 newPosition = Camera.main.transform.position.y;
public float playerDistance = 1.0F; //playerDistance from camera!
void Update ()
{
if (axes == RotationAxes.MouseXAndY) { if(Input.GetButton("Fire2")){ x += Input.GetAxis("Mouse X") 250.0f 0.02f; y -= Input.GetAxis("Mouse Y") 500.0f 0.02f; } y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
// Vector3 position= rotation * new Vector3(0.0f, playerDistance, -distance) + target.position;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
transform.rotation = rotation;
// transform.position = rotation * 1 + target.position;
}
else if (axes == RotationAxes.MouseX)
{
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
}
public static float ClampAngle (float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp (angle, min, max);
}
}