i want to make my camera rotates around selected object, very similiar to the unitys axis gizmo in the top right corner. select object, press f to focus it, then by pressing axis in the top right corner of editor view camera goes from one view to another. how to do that?
i was thinking about quarternions, that seemed too complicated, then i was thinking about rotated around, that seemed too simple, the point is that i dont know nothing about rotations, and i need some directions about this.
you mean like controllable rotation, Right, I would recommend the MouseOrbit script.
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
@AddComponentMenu("Camera-Control/Mouse Orbit")
partial class MouseOrbit { }
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
Here is the script for the mobile version and i changed to c#:
//target object
public Transform target;
//set the distance
public float distance = 10.0f;
//rotation speed for x an y
public float xSpeed = 100.0f;
public float ySpeed = 100.0f;
//set the zoom speed
public float zoomspeed = 10.0f;
//set the minimum and maximum value of zoom
public float zoomMin, zoomMax;
//set the minimum and maximum value of y
public float yMinLimit, yMaxLimit;
//x and y variable
private float x, y;
//set the vector2 value
private Vector2 VecCur, PreCur;
//set the magnitude and zval
private float mag, zval;
//set the position value
private Vector3 position;
//set the variable of roation
private Quaternion rotation;
void Start () {
//set the default x and y value
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
void FixedUpdate () {
//check the target is assigned
if (target) {
//rotate the camera here
if(Input.touchCount == 1)
{
if(Input.GetTouch(0).phase == TouchPhase.Moved)
{
//get the x and y position value
x += Input.GetTouch(0).deltaPosition.x * Time.deltaTime * xSpeed;
y -= Input.GetTouch(0).deltaPosition.y * Time.deltaTime * ySpeed;
//clamp the value here
y = ClampAngle(y, yMinLimit, yMaxLimit);
}
}
//assign the x and y value of rotaion here
rotation = Quaternion.Euler(y, x, 0);
//zoom the camera here
if(Input.touchCount == 2)
{
if(Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
{
//camera current positon
VecCur = Input.GetTouch(0).position - Input.GetTouch(1).position;
//camera previous position
PreCur = ((Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition) - (Input.GetTouch(1).position - Input.GetTouch(1).deltaPosition));
//calculate the magnitude value
mag = VecCur.magnitude - PreCur.magnitude;
//zoom out if the magnitude value is greater than 1
if(mag + 1 > 1)
{
distance -= Time.deltaTime * zoomspeed;
}
//zoom out if the magnitude value is less than 1
if(mag + 1 <= 1)
{
distance += Time.deltaTime * zoomspeed;
}
//clamp the distance here
distance = Mathf.Clamp(distance, zoomMin, zoomMax);
//translate or zoom the camera here
transform.Translate(0, 0, distance);
}
}
//set the camera final position here
position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
//assign the camera final rotaion and position here
transform.rotation = rotation;
transform.position = position;
}
}
//funciton to restrit the value from minimum to maximum
static float ClampAngle (float angle, float min, float max ) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}