- Home /
rotating camera around object -switching different axis views - similiar to unity gizmo
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.
any help appreciated.
Answer by Not showing my name · Aug 15, 2010 at 10:35 PM
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);
}
That should get you started! Happy making!
actually, i managed to do it using mouse orbit. i was using this script before i got the idea about rotating camera this way. it comes down to controlling x for horizontal and y for vertical rotation. so lerping from 0 to 90 (for x) gives rotation of the camera for 90 degress. thanks for pointing this out.
I wish to see how to rotate XY along the local view point ins$$anonymous$$d of the global XY so whenever you rotate around, it goes around... Would anyone know how to do it?
may i ask where should this script be put ? to the camera or the object ?
is this only the script? or should i create another one for the character? im kinda confused ,, sorry
Answer by cregox · Sep 21, 2011 at 09:14 PM
Adding to noname's answer, here's the way to orbit I meant, illustrated by ***superlol*** and solved by **astorga**.
The difference here is the first method is using Euler for the delta, while the second is using Quaternions:
Unity's Traditional
x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
var rotation = Quaternion.Euler(y, x, 0);
or
var rotation = Quaternion.AngleAxis(x, Vector3.up);
rotation *= Quaternion.AngleAxis(y, Vector3.right);
![front rotate](http://i.imgur.com/VKiib.jpg) ![top spin instead of rotate](http://i.imgur.com/A39fi.jpg)
Regular Spatial
x = Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
y = -Input.GetAxis("Mouse Y") * ySpeed * 0.02;
var rotation *= Quaternion.Euler(y, x, 0);
![front rotate](http://i.imgur.com/VKiib.jpg) ![top rotate](http://i.imgur.com/WKEf5.jpg)
var rotation *= Quaternion.Euler(y, x, 0); makes no sense, you cannot multiply something you have not defined yet with itself
Answer by newonepapa · Jan 29, 2015 at 11:05 AM
You can try "gameObject.transform.RotateAround(Target.position, Vector3.up, rotate);"
Answer by schetty · Sep 04, 2015 at 01:12 PM
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);
}
Your answer
Follow this Question
Related Questions
Quaternion Slerp Sanity Check 1 Answer
how do i make first person character rotate left and right along with camera? 0 Answers
Override Oculus Orientation 5.3 1 Answer
1st person Camera rotates suddenly in another direction when unparenting from a platform 1 Answer
How to make the camera rotate only when right click is held down on the mouse? 1 Answer